In continuation of my previous blog on “7. DevOps: How to track changes in a container”, in this blog I would like to show some lab practice “How to control and operate docker containers”.
Controlling/operating Docker container:
In this exercise initially, we can see on how to start/stop/restart the containers.
The Docker Engine enables us to start, stop, and restart a container with a set of docker subcommands.
Let me display the docker images:
=======================>
vskumar@ubuntu:~$ sudo service docker status
● docker.service – Docker Application Container Engine
Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: e
Active: active (running) since Sat 2017-11-25 15:09:35 PST; 2min 24s ago
Docs: https://docs.docker.com
Main PID: 1356 (dockerd)
Tasks: 30
Memory: 95.2M
CPU: 3.998s
=========================>
vskumar@ubuntu:~$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest f2a91732366c 4 days ago 1.85kB
ubuntu 16.04 20c44cd7596f 8 days ago 123MB
ubuntu latest 20c44cd7596f 8 days ago 123MB
busybox latest 6ad733544a63 3 weeks ago 1.13MB
busybox 1.24 47bcc53f74dc 20 months ago 1.11MB
vskumar@ubuntu:~$
=======================>
Now, I want to launch our container ubuntu 16.04 with start subcommand and experiment with the docker stop subcommand, as given below:
$ sudo docker run -i -t ubuntu:16.04 /bin/bash ======================> vskumar@ubuntu:~$ vskumar@ubuntu:~$ sudo docker run -i -t ubuntu:16.04 /bin/bash root@d10ad2bd62f7:/# ======================> Now, we are with this container in interactive mode. Let us apply some linux commands as below: ========================> root@d10ad2bd62f7:/# pwd / root@d10ad2bd62f7:/# ls bin dev home lib64 mnt proc run srv tmp var boot etc lib media opt root sbin sys usr root@d10ad2bd62f7:/# cd home root@d10ad2bd62f7:/home# ls root@d10ad2bd62f7:/home# cd ../var root@d10ad2bd62f7:/var# ls backups cache lib local lock log mail opt run spool tmp root@d10ad2bd62f7:/var# cd tmp root@d10ad2bd62f7:/var/tmp# pwd /var/tmp root@d10ad2bd62f7:/var/tmp# ls root@d10ad2bd62f7:/var/tmp# cd ../lib root@d10ad2bd62f7:/var/lib# ls apt dpkg initscripts insserv misc pam systemd update-rc.d urandom root@d10ad2bd62f7:/var/lib# ================================> Now I want to create a file as below in this container: ==================> root@d10ad2bd62f7:/var/lib# pwd /var/lib root@d10ad2bd62f7:/var/lib# cd ../../home root@d10ad2bd62f7:/home# ls root@d10ad2bd62f7:/home# touch file1.txt ===================> Let me add some text into this file as below: ==========> root@d10ad2bd62f7:/home# echo " Testing containers " > file1.txt root@d10ad2bd62f7:/home# echo " Applying stop command on containers " > file1.txt root@d10ad2bd62f7:/home# cat file1.txt Applying stop command on containers root@d10ad2bd62f7:/home# echo " Testing containers " > file1.txtroot@d10ad2bd62f7:/home# echo " Applying stop command on containers " >> file1.txt root@d10ad2bd62f7:/home# ls file1.txt root@d10ad2bd62f7:/home# ls -l total 4 -rw-r--r-- 1 root root 59 Nov 25 23:20 file1.txt root@d10ad2bd62f7:/home# cat file1.txt Testing containers Applying stop command on containers root@d10ad2bd62f7:/home# ===============> I have applied some more linux file operations on this container as below: =================> root@d10ad2bd62f7:/home# root@d10ad2bd62f7:/home# cat file1.txt >> file2.txt root@d10ad2bd62f7:/home# ls file1.txt file2.txt root@d10ad2bd62f7:/home# ls -l total 8 -rw-r--r-- 1 root root 59 Nov 25 23:20 file1.txt -rw-r--r-- 1 root root 59 Nov 25 23:22 file2.txt root@d10ad2bd62f7:/home# diff file1.txt file2.txt root@d10ad2bd62f7:/home# echo " Applying restart command also on containers " >> file1.txt root@d10ad2bd62f7:/home# ls -l total 8 -rw-r--r-- 1 root root 105 Nov 25 23:23 file1.txt -rw-r--r-- 1 root root 59 Nov 25 23:22 file2.txt root@d10ad2bd62f7:/home# diff file1.txt file2.txt3d2 < Applying restart command also on containers root@d10ad2bd62f7:/home# ====================> Now, let me apply a stop command on this container and see as below by using exit to come out and stop: =====================> root@155f4b0764b1:/# root@155f4b0764b1:/# exit exit vskumar@ubuntu:~$ sudo docker images REPOSITORY TAG IMAGE ID CREATED SIZE hello-world latest f2a91732366c 4 days ago 1.85kB ubuntu 16.04 20c44cd7596f 8 days ago 123MB ubuntu latest 20c44cd7596f 8 days ago 123MB busybox latest 6ad733544a63 3 weeks ago 1.13MB busybox 1.24 47bcc53f74dc 20 months ago 1.11MB vskumar@ubuntu:~$ sudo docker stop d10ad2bd62f7 d10ad2bd62f7 vskumar@ubuntu:~$ =============> Now, I want to check the containers status using ps -a command as below: ==============> vskumar@ubuntu:~$ sudo docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 155f4b0764b1 ubuntu:16.04 "/bin/bash" 2 minutes ago Exited (0) 2 minutes ago zen_volhard cb1ff260d48e ubuntu "ls /usr/src" 10 hours ago Exited (0) 10 hours ago wonderful_hawking b20691fd8fb5 ubuntu "ls /usr" 10 hours ago Exited (0) 10 hours ago friendly_mirzakhani 431ba4c53028 ubuntu "ls" 10 hours ago Exited (0) 10 hours ago affectionate_nobel 2c31684bb1f4 ubuntu "ls -la" 10 hours ago Exited (0) 10 hours ago zealous_meitner fe2e3b449daf ubuntu "ls -la /home/." 10 hours ago Exited (0) 10 hours ago dreamy_shirley c44bdd05b94d ubuntu "ls -la home." 10 hours ago Exited (2) 10 hours ago elastic_pasteur 8b8afa82859a ubuntu "ls -la" 10 hours ago Exited (0) 10 hours ago festive_panini 2811eb37af61 ubuntu "ls -la 604831dbce2a" 10 hours ago Exited (2) 10 hours ago jolly_swartz 604831dbce2a ubuntu:16.04 "/bin/bash" 10 hours ago Exited (0) 10 hours ago vibrant_ride 718636415a7f ubuntu:16.04 "/bin/bash" 11 hours ago Exited (0) 10 hours ago reverent_noyce 53a7751d4673 ubuntu:16.04 "/bin/bash" 12 hours ago Exited (0) 12 hours ago musing_chandrasekhar 32bc16b508d4 ubuntu "bash" 13 hours ago Exited (0) 13 hours ago eager_goldberg 1dd55efde43f hello-world "/hello" 13 hours ago Exited (0) 13 hours ago peaceful_pasteur a744246ffb8e hello-world "/hello" 15 hours ago Exited (0) 15 hours ago naughty_wing 1ba71598b7b8 hello-world "/hello" 15 hours ago Exited (0) 15 hours ago musing_kare vskumar@ubuntu:~$ ===================> you can see the latest status of our container; 155f4b0764b1 ubuntu:16.04 "/bin/bash" 2 minutes ago Exited (0) 2 minutes ago zen_volhard It means Docker maintains in the logs on the usage of containers also. Now, I want to start the previously stopped container using the docker start subcommand by specifying the container ID as an argument, as follows: $ sudo docker start 155f4b0764b1 ===============> vskumar@ubuntu:~$ sudo docker start 155f4b0764b1 155f4b0764b1 vskumar@ubuntu:~$ ===============> Let us check the images status also as below: ==================> Copied the 1st two lines only -----> vskumar@ubuntu:~$ sudo docker ps -a |more CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 155f4b0764b1 ubuntu:16.04 "/bin/bash" 10 minutes ago Up About a minute zen_volhard 11e293722c64 ubuntu:16.04 "/bin/bash" 12 minutes ago Exited (0) 12 minutes ago ====================> It means it shows the current status of the container id:155f4b0764b1 We need to notice one thing here.
By default, the docker start subcommand will not attach to the container.
We can attach it to the container either using the -a option in the docker start subcommand or by explicitly using the docker attach subcommand.
Now let us try these options.
We will see attach command
$ sudo docker attach 155f4b0764b1 =================> vskumar@ubuntu:~$ vskumar@ubuntu:~$ sudo docker attach 155f4b0764b1 root@155f4b0764b1:/# root@155f4b0764b1:/# =================> So the attach command brought the container into interactive mode. Now let me exit it and try the -a option with docker start command: ==================> root@155f4b0764b1:/home# root@155f4b0764b1:/home# exit exit ===============> with start -a option: =============> vskumar@ubuntu:~$ sudo docker start -a 155f4b0764b1 root@155f4b0764b1:/# =================> After exit, I have tried ps command: =====================> vskumar@ubuntu:~$ sudo docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 155f4b0764b1 ubuntu:16.04 "/bin/bash" 21 minutes ago Up 3 minutes zen_volhard vskumar@ubuntu:~$ ======================> From the above display you can see that its start and current status. It means the container is active and running. Now, I want to make another [below] container active. 1dd55efde43f hello-world "/hello" 13 hours ago Exited (0) 13 hours ago peaceful_pasteur Let us see the ps command after these 2 containers are in active state. I want to use the below command: $ sudo docker start -a 1dd55efde43f ===================> vskumar@ubuntu:~$ sudo docker start -a 1dd55efde43f Hello from Docker! This message shows that your installation appears to be working correctly. To generate this message, Docker took the following steps: 1. The Docker client contacted the Docker daemon. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub. (amd64) 3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading. 4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal. Let us try something more ambitious, we can run an Ubuntu container with: $ docker run -it ubuntu bash If you want to; Share images, automate workflows, and more with a free Docker ID: visit: https://cloud.docker.com/ For more examples and ideas, visit: https://docs.docker.com/engine/userguide/ vskumar@ubuntu:~$ ===================> Please note the above container doesn't have a any os related process to keep running continuously. Just it displays the message only. Hence in the list it will not appear. Now, let me list the current processes using docker ps command: ===========> vskumar@ubuntu:~$ sudo docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 155f4b0764b1 ubuntu:16.04 "/bin/bash" 32 minutes ago Up 14 minutes zen_volhard vskumar@ubuntu:~$ ==============> So as on now one container is running.
The next set of container controlling subcommands are docker pause and docker unpause.
The docker pause subcommand will freeze the execution of all the processes within the container.
The docker unpause subcommand will unfreeze the execution of all the processes within the container and resume the execution from the point where it was frozen.
Let us try the below command $sudo docker pause 155f4b0764b1 ========================> vskumar@ubuntu:~$ vskumar@ubuntu:~$ sudo docker pause 155f4b0764b1 155f4b0764b1 vskumar@ubuntu:~$ sudo docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 155f4b0764b1 ubuntu:16.04 "/bin/bash" About an hour ago Up 30 minutes (Paused) zen_volhard vskumar@ubuntu:~$ ==========================> You can see the current status as Paused. Now let me try unpause command also. $ sudo docker unpause 155f4b0764b1 You can see the total output of this container with pause and unpause statuses: ===================> vskumar@ubuntu:~$ vskumar@ubuntu:~$ sudo docker pause 155f4b0764b1 155f4b0764b1 vskumar@ubuntu:~$ sudo docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 155f4b0764b1 ubuntu:16.04 "/bin/bash" About an hour ago Up 30 minutes (Paused) zen_volhard vskumar@ubuntu:~$ ^C vskumar@ubuntu:~$ sudo docker unpause 155f4b0764b1 155f4b0764b1 vskumar@ubuntu:~$ sudo docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 155f4b0764b1 ubuntu:16.04 "/bin/bash" About an hour ago Up 32 minutes zen_volhard vskumar@ubuntu:~$ ======================> Now, in this lab session finally we will use the stop command:
The container and the script running within it can be stopped using the docker stop subcommand, as shown below:
$ sudo docker stop 155f4b0764b1 =====================> vskumar@ubuntu:~$ sudo docker stop 155f4b0764b1 155f4b0764b1 vskumar@ubuntu:~$ sudo docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES =============== It shows there is no active container =============> Now, let me try with -a and more options. =========== Partial display is shown here upto the container ===========> vskumar@ubuntu:~$ sudo docker ps -a |more CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f123dbd09116 ubuntu:16.04 "/bin/bash" 6 minutes ago Exited (0) 5 minutes ago elastic_nightingale 3cfdea29ce6e ubuntu "bash" 14 minutes ago Exited (0) 14 minutes ago gallant_nobel 155f4b0764b1 ubuntu:16.04 "/bin/bash" About an hour ag o Exited (0) 17 seconds ago ================================>
So far in this lab session, we have seen the differences of different commands to operate and control the containers. I would like to break this session for now. In the next blog we will see on how to manage “Housekeeping containers“.