A container is an instance of an image. When we “run” the image, a container is created. Let’s see what happens when we do this.
In previous posts I covered installing the Windows Containers feature, and downloading a base image of Windows Nano Server or Windows Server Core.
Docker is the daemon or service that manages images and containers. It is managed at the command line, in PowerShell or the Command Prompt. To use Windows Containers we need to get familiar with the Docker commands.
I am using the base image of Nano Server as an example, because it is simple and small. When you see how it works, it is easy to imagine how other images based on Windows Server Core might also work.
Lets just run the Nano Server image and see what happens. In an elevated PowerShell console:
docker run microsoft/nanoserver
The console blinks, briefly changes to C:\ prompt, then returns to the PowerShell prompt. It seems that nothing happened at all!
We can try:
docker container ls
to see if any containers exist. It shows none. But:
docker container ls -a
(or –all) shows a container that has exited.
So there was a container but it exited. I can see that the container has an ID, and a name.
I can start the container with:
docker start [ID]
but it exits again. Clearly it is configured to do nothing and then exit.
This container is no use to me, since it just runs and exits. I can remove it with:
docker container rm [ID]
How can I get it to hang around, so I can see what it is? Normally a server waits for something to do, but this container seems to exit if it has nothing to do. It acts more like a process than a server. I could try giving it a command that continues until stopped, like:
docker run microsoft/nanoserver ping -t 18.104.22.168
Now I can see that the container continues to perform the ping. If I disconnect the terminal with Ctrl+C, the container is still running.
If I run:
docker container attach [ID]
then the PowerShell console attaches again to the output of the running ping process.
I need to run:
docker container stop [ID]
to stop the ping process, and:
docker container rm [ID]
to remove the container.
If I want to run a container and see what it is doing, then I can run an interactive container. Putting the commands together:
- create the container:
docker run [image]
- remove it when it exits:
- double dash is for a full word, or an abbreviation:
- single dash is for a concatenation:
- so -[interactive][tty] is:
- give the container a name so that I don’t have to find the ID or the random name:
--name [friendly name]
- a command parameter at the end is the executable to run in the container:
docker run --rm -it --name nano microsoft/nanoserver powershell
gives me a running container with an attached PowerShell console:
Now we can see the inside of the container as well as the outside, and get a good idea of how it works.