Interactive Containers

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

  • Launch an interactive shell in a new or existing container
  • Run a child process inside a running container
  • List containers using more options and filters

Writing to Containers

  1. Create a container using the microsoft/nanoserver:latest image, and connect to its powershell shell in interactive mode using the -i flag (also the -t flag, to request a TTY connection):

    PS: node-0 Administrator> docker container run `
        -it microsoft/nanoserver:latest powershell
    
  2. Explore your container's filesystem with ls, and then create a new file:

    PS C:\> ls
    PS C:\> echo 'hello world' > test.txt
    PS C:\> ls
    PS C:\> type .\test.txt
    
  3. Exit the connection to the container:

    PS C:\> exit
    
  4. Run the same command as above to start a container in the same way:

    PS: node-0 Administrator> docker container run `
        -it microsoft/nanoserver:latest powershell
    
  5. Try finding your test.txt file inside this new container; it is nowhere to be found. Exit this container for now in the same way you did above.

Reconnecting to Containers

  1. We'd like to recover the information written to our container in the first example, but starting a new container didn't get us there; instead, we need to restart our original container, and reconnect to it. List all your stopped containers:

    PS: node-0 Administrator> docker container ls -a
    
    CONTAINER ID   IMAGE                         COMMAND        CREATED              STATUS
    6e76d5dfaf6f   microsoft/nanoserver:latest   "powershell"   44 seconds ago       Exited (0) 32 seconds ago
    bd80127cc68a   microsoft/nanoserver:latest   "powershell"   About a minute ago   Exited (0) 52 seconds ago
    
  2. We can restart a container via the container ID listed in the first column. Use the container ID for the first microsoft/nanoserver container you created with powershell as its command (see the CREATED column above to make sure you're choosing the first powershell container you ran):

    PS: node-0 Administrator> docker container start <container ID>
    PS: node-0 Administrator> docker container ls
    
    CONTAINER ID   IMAGE                         COMMAND       CREATED        STATUS            
    bd80127cc68a   microsoft/nanoserver:latest   "powershell"  5 minutes ago  Up About a minute
    

    Your container status has changed from Exited to Up, via docker container start.

  3. Now that your container is running again, launch a powershell process as a child process inside the container:

    PS: node-0 Administrator> docker container exec -it <container ID> powershell
    
  4. List the contents of the container's filesystem again with ls; your test.txt should be where you left it. Double check that it's content is what you expect:

    PS C:\> type .\test.txt
    
  5. Exit the container again by typing exit.

Using Container Listing Options

  1. In the last step, we saw how to get the short container ID of all our containers using docker container ls -a. Try adding the --no-trunc flag to see the entire container ID:

    PS: node-0 Administrator> docker container ls -a --no-trunc
    

    This long ID is the same as the string that is returned after starting a container with docker container run.

  2. List only the container ID using the -q flag:

    PS: node-0 Administrator> docker container ls -a -q
    
  3. List the last container to have been created using the -l flag:

    PS: node-0 Administrator> docker container ls -l
    
  4. Finally, you can also filter results with the --filter flag; for example, try filtering by exit code:

    PS: node-0 Administrator> docker container ls -a --filter "exited=0"
    

    The output of this command will list the containers that have exited successfully.

  5. Clean up with:

    PS: node-0 Administrator> docker container rm -f $(docker container ls -aq)
    

Conclusion

In this demo, you saw that files added to a container's filesystem do not get added to all containers created from the same image; changes to a container's filesystem are local to itself, and exist only in that particular container. You also learned how to restart a stopped Docker container using docker container start, how to run a command in a running container using docker container exec, and also saw some more options for listing containers via docker container ls.