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
Create a container using the
centos:7image, and connect to its bash shell in interactive mode using the-iflag (also the-tflag, to request a TTY connection):[centos@node-0 ~]$ docker container run -it centos:7 bashExplore your container's filesystem with
ls, and then create a new file:[root@2b8de2ffdf85 /]# ls -l [root@2b8de2ffdf85 /]# echo 'Hello there...' > test.txt [root@2b8de2ffdf85 /]# ls -lExit the connection to the container:
[root@2b8de2ffdf85 /]# exitRun the same command as above to start a container in the same way:
[centos@node-0 ~]$ docker container run -it centos:7 bashTry finding your
test.txtfile 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
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:
[centos@node-0 ~]$ docker container ls -a CONTAINER ID IMAGE COMMAND CREATED STATUS ... cc19f7e9aa91 centos:7 "bash" About a minute ago Exited (0) About a minute ago ... 2b8de2ffdf85 centos:7 "bash" 2 minutes ago Exited (0) About a minute ago ... ...We can restart a container via the container ID listed in the first column. Use the container ID for the first
centos:7container you created withbashas its command (see theCREATEDcolumn above to make sure you're choosing the first bash container you ran):[centos@node-0 ~]$ docker container start <container ID> [centos@node-0 ~]$ docker container ls CONTAINER ID IMAGE COMMAND CREATED STATUS ... 2b8de2ffdf85 centos:7 "bash" 5 minutes ago Up 21 seconds ...Your container status has changed from
ExitedtoUp, viadocker container start.Run
ps -efinside the container you just restarted using Docker'sexeccommand (execruns the specified process as a child of the PID 1 process inside the container):[centos@node-0 ~]$ docker container exec <container ID> ps -efWhat process is PID 1 inside the container? Find the PID of that process on the host machine by using:
[centos@node-0 ~]$ docker container top <container ID>Launch a bash shell in your running container with
docker container exec:[centos@node-0 ~]$ docker container exec -it <container ID> bashList the contents of the container's filesystem again with
ls -l; yourtest.txtshould be where you left it. Exit the container again by typingexit.
Using Container Listing Options
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-truncflag to see the entire container ID:[centos@node-0 ~]$ docker container ls -a --no-truncThis long ID is the same as the string that is returned after starting a container with
docker container run.List only the container ID using the
-qflag:[centos@node-0 ~]$ docker container ls -a -qList the last container to have been created using the
-lflag:[centos@node-0 ~]$ docker container ls -lFinally, you can also filter results with the
--filterflag; for example, try filtering by exit code:[centos@node-0 ~]$ docker container ls -a --filter "exited=0"The output of this command will list the containers that have exited successfully.
Clean up with:
[centos@node-0 ~]$ 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.