Running & Inspecting Containers

By the end of this exercise, you should be able to:

  • Start a container
  • List running and stopped containers

Running Containers

  1. 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:latest part 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 use docker container run.

  2. 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-Process
    

    We get a list of only the processes running inside this container. Try doing Get-Process on the host; of course there is a much longer list of processes here, most of which were hidden from Get-Process running inside our container.

Listing Containers

  1. Try listing all your currently running containers:

    PS: node-0 Administrator> docker container ls
    

    There's nothing listed, since the containers you ran executed a single command, and shut down when finished.

  2. List stopped as well as running containers with the -a flag:

    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 STATUS column.

    Where did those names come from? We truncated the above output table, but in yours you should also see a NAMES column 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 --name flag in docker container run.

  3. 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.