Running & Inspecting Containers
By the end of this exercise, you should be able to:
- Start a container
- List running and stopped containers
Running Containers
First, let's start a container, and observe the output:
PS: node-0 Administrator> docker container run microsoft/nanoserver:latest ` powershell write-host "hello world"The
microsoft/nanoserver:latestpart of the command indicates the image we want to use to define this container; it defines a private filesystem for the container.write-host "hello world"is the process we want to execute inside the kernel namespaces created when we usedocker container run.Now create another container from the same image, and run a different process inside of it:
PS: node-0 Administrator> docker container run ` microsoft/nanoserver:latest powershell Get-ProcessWe get a list of only the processes running inside this container. Try doing
Get-Processon the host; of course there is a much longer list of processes here, most of which were hidden fromGet-Processrunning inside our container.
Listing Containers
Try listing all your currently running containers:
PS: node-0 Administrator> docker container lsThere's nothing listed, since the containers you ran executed a single command, and shut down when finished.
List stopped as well as running containers with the
-aflag:PS: node-0 Administrator> docker container ls -a CONTAINER IMAGE COMMAND STATUS ... cfdabf6928c7 microsoft/nanoserver:latest "powershell Get-Pr..." Exited (0) 2 minutes ago ... 9c61fad09332 microsoft/nanoserver:latest "powershell write-..." Exited (0) 4 minutes ago ...We can see our exited containers this time, with a time and exit code in the
STATUScolumn.Where did those names come from? We truncated the above output table, but in yours you should also see a
NAMEScolumn with some funny names. All containers have names, which in most Docker CLI commands can be substituted for the container ID as we'll see in later exercises. By default, containers get a randomly generated name of the form<adjective>_<scientist / technologist>, but you can choose a name explicitly with the--nameflag indocker container run.Clean up all containers using this command:
PS: node-0 Administrator> docker container rm -f $(docker container ls -aq)Please discuss with your peers what the above command exactly does.
Conclusion
In this exercise you ran your first container using docker container run, and explored the importance of the main process in a container; this process is a member of the host's list of processes like any other, but is 'containerized' via tools like kernel namespaces, making this process and other processes running in the container behave as if they were the only processes running on the system. Furthermore, this process defines the state of the container; if the process exits, the container stops.