Using Docker – Useful Commands

It is Saturday morning in the Twin Cities of Minneapolis and St. Paul. Our current temperature is 30 F and will go up a few degrees later on just before it starts raining. The rain will change to snow and the winds will pick up. It should end Sunday mid morning. When all is set and done we could get about seven inches of fresh heavy snow. Lovely isn’t it?

BTW, tomorrow daylight savings starts. Do not forget to move your clocks forward. It is hard to believe that spring is just a couple weeks from today. We have had a cold and snowy winter. I believe most people in this part of the country have had enough of it.

In this post we will cover some commonly used Docker commands. For a full set you should check the Docker documentation.

# **** see the full layers that make the redis image ****
$ sudo service docker start
[sudo] password for johncanessa: 
Redirecting to /bin/systemctl start docker.service

$ docker images
REPOSITORY           TAG                 IMAGE ID            CREATED             SIZE
redis                latest              0f88f9be5839        2 days ago          95MB
johncanessa/cowsay   latest              30d74744acb1        9 days ago          131MB
debian               latest              d508d16c64cd        4 weeks ago         101MB

$ docker history redis
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
0f88f9be5839        2 days ago          /bin/sh -c #(nop)  CMD ["redis-server"]         0B                  
<missing>           2 days ago          /bin/sh -c #(nop)  EXPOSE 6379                  0B                  
<missing>           2 days ago          /bin/sh -c #(nop)  ENTRYPOINT ["docker-entry…   0B                  
<missing>           2 days ago          /bin/sh -c #(nop) COPY file:df205a0ef6e6df89…   374B                
<missing>           2 days ago          /bin/sh -c #(nop) WORKDIR /data                 0B                  
<missing>           2 days ago          /bin/sh -c #(nop)  VOLUME [/data]               0B                  
<missing>           2 days ago          /bin/sh -c mkdir /data && chown redis:redis …   0B                  
<missing>           2 days ago          /bin/sh -c set -ex;   buildDeps='   ca-certi…   36.4MB              
<missing>           2 days ago          /bin/sh -c #(nop)  ENV REDIS_DOWNLOAD_SHA=e2…   0B                  
<missing>           2 days ago          /bin/sh -c #(nop)  ENV REDIS_DOWNLOAD_URL=ht…   0B                  
<missing>           2 days ago          /bin/sh -c #(nop)  ENV REDIS_VERSION=5.0.3      0B                  
<missing>           2 days ago          /bin/sh -c set -ex;   fetchDeps="   ca-certi…   3MB                 
<missing>           2 days ago          /bin/sh -c #(nop)  ENV GOSU_VERSION=1.10        0B                  
<missing>           2 days ago          /bin/sh -c groupadd -r redis && useradd -r -…   329kB               
<missing>           2 days ago          /bin/sh -c #(nop)  CMD ["bash"]                 0B                  
<missing>           2 days ago          /bin/sh -c #(nop) ADD file:5ea7dfe8c8bc87ebe…   55.3MB              

We first start the docker service. Without it there is not much we can do. After listing the images we have in my local repository we take a look at the different layers in the Redis image. To read about Redis you can do it here. The history shows in order the layers that have been added to build the image. We can flatten it as we will see later on in this post.

# **** launch layer before failure ****
FROM busybox:latest
RUN echo "this should work"
RUN /bin/bash echo "this should NOT work"

$ docker build -t echohost .
Sending build context to Docker daemon  2.048kB
Step 1/3 : FROM busybox:latest
latest: Pulling from library/busybox
697743189b6d: Pull complete 
Digest: sha256:061ca9704a714ee3e8b80523ec720c64f6209ad3f97c0ff7cb9ec7d19f15149f
Status: Downloaded newer image for busybox:latest
 ---> d8233ab899d4				<=== busybox image ID Step 2/3 : RUN echo "this should work" ---> Running in 631f4654e3d0	<=== this worked this should work Removing intermediate container 631f4654e3d0 ---> 156c579eaeb0				<=== this failed Step 3/3 : RUN /bin/bash echo "this should NOT work" ---> Running in 3de8d6fa8b28
/bin/sh: /bin/bash: not found
The command '/bin/sh -c /bin/bash echo "this should NOT work"' returned a non-zero code: 127

$ docker run -it 156c579eaeb0	<=== this failed
/ # /bin/bash -c "echo testing"
sh: /bin/bash: not found
/ # /bin/sh -c "echo got it !!!"
got it !!!
/ # exit
$ 

When you run into a startup issue with a container chances are that the sequence encountered an issue. In this last example we use a Dockerfile to build an image and run a couple commands. It seems that it should work but is does not. In this simple case, the issue is indicated by the last line generated by the docker build command. It is easy to tell that we invoked the bash shell but our image lacks such shell. In some cases the issue might be more cryptic.

A simple approach to debug the Dockerfile is to run the last layer that worked and from that point on issue the offending command. That should provide additional information to address the issue.

In our example we start with the image that failed and issue the echo command to be executed in the bash shell.

# **** forward port 8000 on host to port 80 in container ****
$ docker run -d -p 8000:80 nginx
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
f7e2b70d04ae: Already exists 
08dd01e3f3ac: Pull complete 
d9ef3a1eb792: Pull complete 
Digest: sha256:98efe605f61725fd817ea69521b0eeb32bef007af0e3d0aeb6258c6e6fe7fc1a
Status: Downloaded newer image for nginx:latest
866b8e13818d1d5c38f25a8c9b9d149542ccc15d341d2d8cddeaf6f60e911d82

$ docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                         PORTS                  NAMES
866b8e13818d        nginx               "nginx -g 'daemon of…"   20 seconds ago      Up 19 seconds                  0.0.0.0:8000->80/tcp   zen_raman
a6fdd1b5f260        156c579eaeb0        "sh"                     9 minutes ago       Exited (0) 7 minutes ago                              competent_mendeleev
c80c63b6ea17        156c579eaeb0        "sh"                     17 minutes ago      Exited (0) 10 minutes ago                             zen_sanderson
3de8d6fa8b28        156c579eaeb0        "/bin/sh -c '/bin/ba…"   25 minutes ago      Exited (127) 25 minutes ago                           goofy_wright
693d6e4df27a        redis               "docker-entrypoint.s…"   About an hour ago   Exited (0) About an hour ago                          laughing_elion

$ curl localhost:8000
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>

<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-wp-preserve="%3Cstyle%3E%0A%20%20%20%20body%20%7B%0A%20%20%20%20%20%20%20%20width%3A%2035em%3B%0A%20%20%20%20%20%20%20%20margin%3A%200%20auto%3B%0A%20%20%20%20%20%20%20%20font-family%3A%20Tahoma%2C%20Verdana%2C%20Arial%2C%20sans-serif%3B%0A%20%20%20%20%7D%0A%3C%2Fstyle%3E" data-mce-resize="false" data-mce-placeholder="1" class="mce-object" width="20" height="20" alt="&lt;style&gt;" title="&lt;style&gt;" />

</head>
<body>


<h1>Welcome to nginx!</h1>




If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.




For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.




<em>Thank you for using nginx.</em>

</body>
</html>

$ docker kill 866b8e13818d 
866b8e13818d

$ docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                        PORTS               NAMES
866b8e13818d        nginx               "nginx -g 'daemon of…"   4 minutes ago       Exited (137) 7 seconds ago                        zen_raman
a6fdd1b5f260        156c579eaeb0        "sh"                     13 minutes ago      Exited (0) 11 minutes ago                         competent_mendeleev
c80c63b6ea17        156c579eaeb0        "sh"                     21 minutes ago      Exited (0) 14 minutes ago                         zen_sanderson
3de8d6fa8b28        156c579eaeb0        "/bin/sh -c '/bin/ba…"   30 minutes ago      Exited (127) 29 minutes ago                       goofy_wright
693d6e4df27a        redis               "docker-entrypoint.s…"   2 hours ago         Exited (0) 2 hours ago                            laughing_elion

In this last screen capture, we start a container detached from our console. The container holds the Nginx web server. To read more about Nginx you can do it here.

Of interest we asked Docker to forward port 80 in the container to post 8000 in our Linux server. The Docker ps –a command confirms that the port has been forwarded. We then use curl to communicate with Nginx running in the container from our host computer.

# **** automatically select free port to forward to on the host ****
$ ID=$(docker run -d -P nginx)  

$ echo $ID
5009ef3a1fa900b5d239b71b80d5028badf7a14bd926747f4536c2a722ce9c82

$ docker port $ID 80
0.0.0.0:32770

$ curl localhost:32770
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>

<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-wp-preserve="%3Cstyle%3E%0A%20%20%20%20body%20%7B%0A%20%20%20%20%20%20%20%20width%3A%2035em%3B%0A%20%20%20%20%20%20%20%20margin%3A%200%20auto%3B%0A%20%20%20%20%20%20%20%20font-family%3A%20Tahoma%2C%20Verdana%2C%20Arial%2C%20sans-serif%3B%0A%20%20%20%20%7D%0A%3C%2Fstyle%3E" data-mce-resize="false" data-mce-placeholder="1" class="mce-object" width="20" height="20" alt="&lt;style&gt;" title="&lt;style&gt;" />

</head>
<body>


<h1>Welcome to nginx!</h1>




If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.




For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.




<em>Thank you for using nginx.</em>

</body>
</html>

An issue might be to keep track of the open ports in our host. In this example we run the same Nginx container but ask Docker to choose a free port. In this case it chose 32770.  To verify all worked, we issue the curl command and get back the expected results.

# **** docker run with links ****
$ docker run -d --name myredis redis
14e36fc0224df0ad9c588c462fe0a920ad20eea872a48136d020136adbd968cb

$ docker run --link myredis:redis debian env
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=54dbf433b71d
REDIS_PORT=tcp://172.17.0.2:6379
REDIS_PORT_6379_TCP=tcp://172.17.0.2:6379
REDIS_PORT_6379_TCP_ADDR=172.17.0.2
REDIS_PORT_6379_TCP_PORT=6379
REDIS_PORT_6379_TCP_PROTO=tcp
REDIS_NAME=/confident_tharp/redis
REDIS_ENV_GOSU_VERSION=1.10
REDIS_ENV_REDIS_VERSION=5.0.3
REDIS_ENV_REDIS_DOWNLOAD_URL=http://download.redis.io/releases/redis-5.0.3.tar.gz
REDIS_ENV_REDIS_DOWNLOAD_SHA=e290b4ddf817b26254a74d5d564095b11f9cd20d8f165459efa53eb63cd93e02
HOME=/root

$ docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS                           PORTS               NAMES
54dbf433b71d        debian              "env"                    46 seconds ago       Exited (0) 44 seconds ago                            confident_tharp
14e36fc0224d        redis               "docker-entrypoint.s…"   About a minute ago   Up About a minute                6379/tcp            myredis
5009ef3a1fa9        nginx               "nginx -g 'daemon of…"   10 minutes ago       Exited (137) 3 minutes ago                           condescending_kalam
22afb3b7e6c1        nginx               "nginx -g 'daemon of…"   13 minutes ago       Exited (137) 11 minutes ago                          hungry_ride
2eb2ebac5628        nginx               "nginx -g 'daemon of…"   17 minutes ago       Exited (137) 14 minutes ago                          boring_mccarthy
866b8e13818d        nginx               "nginx -g 'daemon of…"   24 minutes ago       Exited (137) 19 minutes ago                          zen_raman
a6fdd1b5f260        156c579eaeb0        "sh"                     33 minutes ago       Exited (0) 31 minutes ago                            competent_mendeleev
c80c63b6ea17        156c579eaeb0        "sh"                     41 minutes ago       Exited (0) 34 minutes ago                            zen_sanderson
3de8d6fa8b28        156c579eaeb0        "/bin/sh -c '/bin/ba…"   About an hour ago    Exited (127) About an hour ago                       goofy_wright
693d6e4df27a        redis               "docker-entrypoint.s…"   2 hours ago          Exited (0) 2 hours ago                               laughing_elion

In this example we start in detached mode our Redis container. We then start a second container and link it to the Redis container. The purpose is to be able to communicate with it. I could have started the second container with a shell “-it” in order to be able to show interaction with the server. I have such scenario illustrated in a previous post.

Note that a lot of communication information is displayed when we start the client container.

# **** Docker run --rm  ****
$ docker run -it --rm debian /bin/bash

root@35541380331d:/# whoami
root

root@35541380331d:/# which bash
/bin/bash

root@35541380331d:/# cat /etc/shells
# /etc/shells: valid login shells
/bin/sh
/bin/dash
/bin/bash
/bin/rbash

root@35541380331d:/# exit
exit

$ docker images -a                    
REPOSITORY           TAG                 IMAGE ID            CREATED             SIZE
nginx                latest              881bd08c0b08        2 days ago          109MB
redis                latest              0f88f9be5839        2 days ago          95MB
johncanessa/cowsay   latest              30d74744acb1        9 days ago          131MB
busybox              latest              d8233ab899d4        2 weeks ago         1.2MB
debian               latest              d508d16c64cd        4 weeks ago         101MB

In the last screen capture we start a container using the –rm flag. We interact with it. This is done thanks to the –it argument. The specified shell is bash specified as /bin/bash. We take a look at the different shells configured in this image. Finally we exit the container. We then take a look at the images. Probably I should have included a “docker ps –aq” command to show that the container was removed.

# **** declaring environment variables ****
$ docker run --rm -e var1=val -e var2="val 2" debian env
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=9bd0d7c57626
var1=val
var2=val 2
HOME=/root

In this past screen capture, we run a container which will be deleted when stopped and declare two environment variables. When the container starts we invoke the env command to verify that the environment variables have been declared.

# **** assigning a name to the host in a container ****
$ docker run -h "myhost" -it --rm debian /bin/bash

root@myhost:/# hostname
myhost

root@myhost:/# cat /etc/shells
# /etc/shells: valid login shells
/bin/sh
/bin/dash
/bin/bash
/bin/rbash

root@myhost:/# exit
exit

$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

We start a container, name the host “myhost”, start an interactive shell with –it, remove the container after it stops and start the bash shell. What a mouthful. Once the container is running we check the host name. Finally we exit the container. This time I check that the container has been removed :o)

# **** attaching to a detached container ****
$ ID=$(docker run -d debian sh -c "while true; do echo 'tic'; sleep 1; echo 'toc'; done;")

$ echo $ID
b907ac0703d078c1e01a0717701cb772e3476fa89cf8e08c32ee2e630f5548b5

$ docker attach $ID
toc
tic
toc
tic
toc
tic
toc
tic
toc
tic
^C

$docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                     PORTS               NAMES
b907ac0703d0        debian              "sh -c 'while true; …"   48 seconds ago      Exited (0) 9 seconds ago                       reverent_minsky

In the last example, we start a container in detached mode. We specify a shell script to run. Note that we collect the container ID. We display the container ID. The next command is used to attach to the running container. Immediately we see that the script is running. We then press control-c. That stops it. Note that we did not specify the –rm flag when the container was started. The container was stopped but was not deleted.

# **** execute a command inside a running container ****
$ ID=$(docker run -d debian sh -c "while true; do sleep 1; done;")                        

$ echo $ID
9eea5fcf021cc50f5c5520be6b4ccd4bd8628b9966fac7373d8ae087fca82e4d

$ docker exec $ID echo "Hello World"
Hello World

$ docker exec -it $ID /bin/bash

root@9eea5fcf021c:/# uname -a
Linux 9eea5fcf021c 3.10.0-957.5.1.el7.x86_64 #1 SMP Fri Feb 1 14:54:57 UTC 2019 x86_64 GNU/Linux

root@9eea5fcf021c:/# whoami
root

root@9eea5fcf021c:/# exit
exit

$ docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                     PORTS               NAMES
9eea5fcf021c        debian              "sh -c 'while true; …"   2 minutes ago       Up 2 minutes                                   wonderful_archimedes
b907ac0703d0        debian              "sh -c 'while true; …"   7 minutes ago       Exited (0) 6 minutes ago                       reverent_minsky

In this past screen capture, we start a detached container. The container does not exit after starting because it will execute a loop sleeping for a second in each pass. We execute an echo command in the running container. We then execute an interactive shell in the running container. We ran two commands and exit. When we check the containers, both exit and are listed using the “docker ps –aq” command.

# **** stop all running containers ****
$ docker container stop $(docker container ls -aq)
9eea5fcf021c
b907ac0703d0

$ docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                       PORTS               NAMES
9eea5fcf021c        debian              "sh -c 'while true; …"   5 minutes ago       Exited (137) 8 seconds ago                       wonderful_archimedes
b907ac0703d0        debian              "sh -c 'while true; …"   10 minutes ago      Exited (0) 10 minutes ago                        reverent_minsky

Not sure if I included this last command in my previous post on Docker. We stop (not delete) all running containers. We verify that the action was properly executed.

# **** send a signal (default: SIGKILL) to a container ****
$ ID=$(docker run -d debian bash -c "trap 'echo caught' SIGTRAP; while true; do sleep 1; done;")

$ echo $ID
4de13833df2b818e2da89b1232602a4f5681efd0e361ea1fa3674c5fba726e3a

$ docker kill -s SIGTRAP $ID
4de13833df2b818e2da89b1232602a4f5681efd0e361ea1fa3674c5fba726e3a

$ docker logs $ID
caught

$ docker kill $ID
4de13833df2b818e2da89b1232602a4f5681efd0e361ea1fa3674c5fba726e3a

$ docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS                       PORTS               NAMES
4de13833df2b        debian              "bash -c 'trap 'echo…"   About a minute ago   Exited (137) 8 seconds ago                       zealous_wozniak
9eea5fcf021c        debian              "sh -c 'while true; …"   12 minutes ago       Exited (137) 7 minutes ago                       wonderful_archimedes
b907ac0703d0        debian              "sh -c 'while true; …"   17 minutes ago       Exited (0) 17 minutes ago                        reverent_minsky

We can send signals to running containers. In this example we start a detached container and using a script we tell it to catch signal and then loop. We loop so the shell does not exit and stops the container. We then send a SIGTRAP signal. The container indicates that the signal was received by looking at the container’s log. We then send a second signal. Since we do not specify one, the default is SIGKILL. We then verify that the container has exited.

At this point in time we have a few exited containers.

# **** delete all stopped containers ****
$ docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                       PORTS               NAMES
4de13833df2b        debian              "bash -c 'trap 'echo…"   4 minutes ago       Exited (137) 2 minutes ago                       zealous_wozniak
9eea5fcf021c        debian              "sh -c 'while true; …"   14 minutes ago      Exited (137) 9 minutes ago                       wonderful_archimedes
b907ac0703d0        debian              "sh -c 'while true; …"   20 minutes ago      Exited (0) 19 minutes ago                        reverent_minsky

$ docker rm $(docker ps -aq)
4de13833df2b
9eea5fcf021c
b907ac0703d0

$ docker ps -a              
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

As you can see we have three containers that have exited. By entering the docker rm command we are able to delete them without the need to manually specify a container ID. This command proves quite useful when developing code for containers.

# **** show changes made to a container file system ****
$ ID=$(docker run -d debian touch /NEW_FILE)                                                    

$ echo $ID
35a1c1e683ddd0d73834d1c5a07cac3028f73c4af006634ffa1ed63adb121003

$ docker diff $ID
A /NEW_FILE

$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
35a1c1e683dd        debian              "touch /NEW_FILE"   33 seconds ago      Exited (0) 32 seconds ago                       pensive_hofstadter

$ docker rm $(docker ps -aq)                
35a1c1e683dd

$ docker ps -a              
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

In this example we start a detached container and create an empty file named “/NEW_FILE”. We use the container’s ID to run a docker diff command. It shows that we have appended a new file named /NEW_FILE. We verify that the container has stopped. The reason is that the only command specified was touch. As soon as the command completed the container exited. We then remove the stopped / exited containers and verify that all is well.

# **** list exposed port mappings for a container ****
$ ID=$(docker run -P -d redis) 

$ echo $ID
d7fa67216c85d87ec110a6b4de9b307b04beadf0795acd59c23b6e8e1363b85c

$ docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                     NAMES
d7fa67216c85        redis               "docker-entrypoint.s…"   13 seconds ago      Up 11 seconds       0.0.0.0:32768->6379/tcp   elated_galois

$ docker port $ID
6379/tcp -> 0.0.0.0:32768

$ docker port $ID 6379
0.0.0.0:32768

$ docker port $ID 6379/tcp
0.0.0.0:32768

$ docker logs $ID
1:C 08 Mar 2019 15:04:00.447 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1:C 08 Mar 2019 15:04:00.448 # Redis version=5.0.3, bits=64, commit=00000000, modified=0, pid=1, just started
1:C 08 Mar 2019 15:04:00.448 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
1:M 08 Mar 2019 15:04:00.452 * Running mode=standalone, port=6379.
1:M 08 Mar 2019 15:04:00.452 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
1:M 08 Mar 2019 15:04:00.452 # Server initialized
1:M 08 Mar 2019 15:04:00.452 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
1:M 08 Mar 2019 15:04:00.452 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
1:M 08 Mar 2019 15:04:00.452 * Ready to accept connections

In this last example we run in detached mode the Redis image. The –P argument tells Docker to assign ports to unmapped ones on our host computer. We verify that the container is running. The docker ps –a shows that Redis default listening port 6379 has been mapped in our computer to port 32768. We obtain the same information is different ways. The last command shows the log and we are able to verify that the Redis port on the container is 6379.

What I missed in this example is to connect to the Redis server using the redis-cli to verify that the forwarded port works. You can see an example in my previous post.

# **** info on running processes in a container ****
$ ID=$(docker run -d redis) 

$ echo $ID                  
df4cb9d3fdbd44b6e1176dd6e838133e924098d4d84d7ab1faa7e869e09101d4

$ docker top $ID
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
polkitd             14002               13983               0                   09:16               ?                   00:00:00            redis-server *:6379

$ ps -f -u polkitd
UID        PID  PPID  C STIME TTY          TIME CMD
polkitd   4853     1  0 08:27 ?        00:00:01 /usr/lib/polkit-1/polkitd --no-debug
polkitd  14002 13983  0 09:16 ?        00:00:00 redis-server *:6379

$ docker top $ID -axZ
LABEL                                       PID                 TTY                 STAT                TIME                COMMAND
system_u:system_r:unconfined_service_t:s0   14002               ?                   Ssl                 0:00                redis-server *:6379

$ docker stop $ID
df4cb9d3fdbd44b6e1176dd6e838133e924098d4d84d7ab1faa7e869e09101d4

$ docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                     PORTS               NAMES
df4cb9d3fdbd        redis               "docker-entrypoint.s…"   2 minutes ago       Exited (0) 7 seconds ago                       angry_aryabhata

$ docker rm $(docker ps -aq)
df4cb9d3fdbd

$ docker ps -a              
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

In this example we started Redis in a detached container. We use the “docker top” command to list the running processes in the specified container. We double check by running ps in the host computer. When done we stop the Redis container, verify it has stopped / exited, remove all stopped containers and verify all is well.

# **** dealing with images: commit (preferred method build) ****
$ ID=$(docker run -d redis touch /new_file)

$ echo $ID
d6255f6930a364bcd8958ff3de30c7f830e9b3c68f13de37177c834b2a5be5e0

$ docker commit -a "My Blogs" -m "Comment" $ID commit:test
sha256:d1c21fa17ef23d88304f7f9e7e1d0afa36e7511c2d93f68fcdf73be5ca2eeae6

$ docker images commit
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
commit              test                d1c21fa17ef2        17 seconds ago      95MB

$ docker run commit:test ls /new_file
/new_file

$ docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS               NAMES
966c6d7a011f        commit:test         "docker-entrypoint.s…"   13 seconds ago      Exited (0) 12 seconds ago                       sad_kapitsa
d6255f6930a3        redis               "docker-entrypoint.s…"   2 minutes ago       Exited (0) 2 minutes ago                        dazzling_boyd

$ docker images -a
REPOSITORY           TAG                 IMAGE ID            CREATED             SIZE
commit               test                d1c21fa17ef2        2 minutes ago       95MB
nginx                latest              881bd08c0b08        3 days ago          109MB
redis                latest              0f88f9be5839        3 days ago          95MB
johncanessa/cowsay   latest              30d74744acb1        10 days ago         131MB
busybox              latest              d8233ab899d4        3 weeks ago         1.2MB
debian               latest              d508d16c64cd        4 weeks ago         101MB

In this example we start a detached Redis container which creates a new empty file named /new_file. We create an image named commit:test. We then commit the image. We run the new image and use the ls command to verify that the /new_file exists. I could have invoked “docker diff” to verify that we did not change the image, but we know that ls will not create the specified file. We then check the status of the containers and list our images.

# **** flattening an image ****
$ docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS               NAMES
966c6d7a011f        commit:test         "docker-entrypoint.s…"   19 minutes ago      Exited (0) 19 minutes ago                       sad_kapitsa
d6255f6930a3        redis               "docker-entrypoint.s…"   21 minutes ago      Exited (0) 21 minutes ago                       dazzling_boyd

$ docker history commit:test
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
d1c21fa17ef2        21 minutes ago      touch /new_file                                 0B                  Comment
0f88f9be5839        3 days ago          /bin/sh -c #(nop)  CMD ["redis-server"]         0B                  
<missing>           3 days ago          /bin/sh -c #(nop)  EXPOSE 6379                  0B                  
<missing>           3 days ago          /bin/sh -c #(nop)  ENTRYPOINT ["docker-entry…   0B                  
<missing>           3 days ago          /bin/sh -c #(nop) COPY file:df205a0ef6e6df89…   374B                
<missing>           3 days ago          /bin/sh -c #(nop) WORKDIR /data                 0B                  
<missing>           3 days ago          /bin/sh -c #(nop)  VOLUME [/data]               0B                  
<missing>           3 days ago          /bin/sh -c mkdir /data && chown redis:redis …   0B                  
<missing>           3 days ago          /bin/sh -c set -ex;   buildDeps='   ca-certi…   36.4MB              
<missing>           3 days ago          /bin/sh -c #(nop)  ENV REDIS_DOWNLOAD_SHA=e2…   0B                  
<missing>           3 days ago          /bin/sh -c #(nop)  ENV REDIS_DOWNLOAD_URL=ht…   0B                  
<missing>           3 days ago          /bin/sh -c #(nop)  ENV REDIS_VERSION=5.0.3      0B                  
<missing>           3 days ago          /bin/sh -c set -ex;   fetchDeps="   ca-certi…   3MB                 
<missing>           3 days ago          /bin/sh -c #(nop)  ENV GOSU_VERSION=1.10        0B                  
<missing>           3 days ago          /bin/sh -c groupadd -r redis && useradd -r -…   329kB               
<missing>           3 days ago          /bin/sh -c #(nop)  CMD ["bash"]                 0B                  
<missing>           3 days ago          /bin/sh -c #(nop) ADD file:5ea7dfe8c8bc87ebe…   55.3MB              

$ docker export 966c6d7a011f | docker import - flatten:test
sha256:aa9ddc7ae6db572747cec513499bd4dc911b4080ba8e06afc944dc1f29f4b054

$ docker history flatten:test
IMAGE               CREATED             CREATED BY          SIZE                COMMENT
aa9ddc7ae6db        25 seconds ago                          91.6MB              Imported from -

When we create an image from an existing container, the different layers are shown. This example illustrates how to flatten an image. We look at the history of the commit:test image. We see it contains over 10 layers. We export the image and run the “docker history” on the flattened image. We now have a single layer.

# **** docker save ****
$ docker save -o /tmp/redis.tar redis:latest

$ ls -l /tmp
total 95984
drwxr-xr-x. 2 root        root              19 Mar  7 12:23 hsperfdata_root
-rw-r--r--. 1 root        root              73 Mar  7 12:23 lua_pcczR2
-rw-------. 1 johncanessa johncanessa 98264576 Mar  8 11:42 redis.tar   <====
drwx------. 2 johncanessa johncanessa       24 Mar  8 08:28 ssh-GGguXn0GVSd5
drwx------. 3 root        root              17 Mar  8 08:28 systemd-private-11026b71a7c74c6397849406c6c58573-bolt.service-4yntVL
drwx------. 3 root        root              17 Mar  8 08:28 systemd-private-11026b71a7c74c6397849406c6c58573-colord.service-Pf7jo7
drwx------. 3 root        root              17 Mar  8 08:27 systemd-private-11026b71a7c74c6397849406c6c58573-cups.service-IDQHWt
drwx------. 3 root        root              17 Mar  8 08:28 systemd-private-11026b71a7c74c6397849406c6c58573-fwupd.service-qLQAYS
drwx------. 3 root        root              17 Mar  8 08:27 systemd-private-11026b71a7c74c6397849406c6c58573-rtkit-daemon.service-RcjCoP
drwx------. 2 johncanessa johncanessa        6 Feb 25 16:53 tracker-extract-files.1000
-rw-------. 1 root        root            4960 Mar  7 09:50 yum_save_tx.2019-03-07.09-50.AXTm2F.yumtx
-rw-------. 1 root        root            4960 Mar  7 12:23 yum_save_tx.2019-03-07.12-23.9WFXSw.yumtx

$ docker rmi -f redis:latest
Untagged: redis:latest
Untagged: redis@sha256:4be7fdb131e76a6c6231e820c60b8b12938cf1ff3d437da4871b9b2440f4e385

$ docker load -i /tmp/redis.tar
Loaded image: redis:latest

$ docker images -a   
REPOSITORY           TAG                 IMAGE ID            CREATED             SIZE
flatten              test                aa9ddc7ae6db        29 minutes ago      91.6MB
commit               test                d1c21fa17ef2        About an hour ago   95MB
nginx                latest              881bd08c0b08        3 days ago          109MB
redis                latest              0f88f9be5839        3 days ago          95MB
johncanessa/cowsay   latest              30d74744acb1        10 days ago         131MB
busybox              latest              d8233ab899d4        3 weeks ago         1.2MB
debian               latest              d508d16c64cd        4 weeks ago         101MB

$ rm -rf /tmp/redis.tar

$ ls -l /tmp
total 20
drwxr-xr-x. 2 root        root          19 Mar  7 12:23 hsperfdata_root
-rw-r--r--. 1 root        root          73 Mar  7 12:23 lua_pcczR2
drwx------. 2 johncanessa johncanessa   24 Mar  8 08:28 ssh-GGguXn0GVSd5
drwx------. 3 root        root          17 Mar  8 08:28 systemd-private-11026b71a7c74c6397849406c6c58573-bolt.service-4yntVL
drwx------. 3 root        root          17 Mar  8 08:28 systemd-private-11026b71a7c74c6397849406c6c58573-colord.service-Pf7jo7
drwx------. 3 root        root          17 Mar  8 08:27 systemd-private-11026b71a7c74c6397849406c6c58573-cups.service-IDQHWt
drwx------. 3 root        root          17 Mar  8 08:28 systemd-private-11026b71a7c74c6397849406c6c58573-fwupd.service-qLQAYS
drwx------. 3 root        root          17 Mar  8 08:27 systemd-private-11026b71a7c74c6397849406c6c58573-rtkit-daemon.service-RcjCoP
drwx------. 2 johncanessa johncanessa    6 Feb 25 16:53 tracker-extract-files.1000
-rw-------. 1 root        root        4960 Mar  7 09:50 yum_save_tx.2019-03-07.09-50.AXTm2F.yumtx
-rw-------. 1 root        root        4960 Mar  7 12:23 yum_save_tx.2019-03-07.12-23.9WFXSw.yumtx

In this example we save the Redis image to a tar file. We then verify that the tar file was created. We remove the redis:latest image from our system.  We then load the Redis image from the tar file we created in a previous step. The The redis:latest image is restored. We then remove the tar file and verify that is has been removed.

There are different ways to interact with Docker volumes. The following three examples tend to achieve the same result.

# **** working with Docker volumes: first approach ****
$ docker images -a                    
REPOSITORY           TAG                 IMAGE ID            CREATED             SIZE
flatten              test                aa9ddc7ae6db        19 hours ago        91.6MB
commit               test                d1c21fa17ef2        19 hours ago        95MB
nginx                latest              881bd08c0b08        4 days ago          109MB
redis                latest              0f88f9be5839        4 days ago          95MB
johncanessa/cowsay   latest              30d74744acb1        11 days ago         131MB
busybox              latest              d8233ab899d4        3 weeks ago         1.2MB
debian               latest              d508d16c64cd        4 weeks ago         101MB
centos               latest              1e1148e4cc2c        3 months ago        202MB

$ docker run -it --name container-test -h CONTAINER -v /data debian /bin/bash
root@CONTAINER:/# ls /data

$ docker inspect container-test                  
:::: :::: ::::
       "Mounts": [
            {
                "Type": "volume",
                "Name": "5707da3b53603494bd23b437ff21dfba52803af64970ba1c03e6671be9c8acd9",
                "Source": "/var/lib/docker/volumes/5707da3b53603494bd23b437ff21dfba52803af64970ba1c03e6671be9c8acd9/_data",
                "Destination": "/data",
                "Driver": "local",
                "Mode": "",
                "RW": true,
                "Propagation": ""
            }
        ],
:::: :::: ::::

$ docker inspect -f {{.Mounts}} container-test
[{volume 5707da3b53603494bd23b437ff21dfba52803af64970ba1c03e6671be9c8acd9 /var/lib/docker/volumes/5707da3b53603494bd23b437ff21dfba52803af64970ba1c03e6671be9c8acd9/_data /data local  true }]

root@CONTAINER:/# cd data

root@CONTAINER:/data# ls -al
total 0
drwxr-xr-x. 2 root root  6 Mar  9 11:53 .
drwxr-xr-x. 1 root root 18 Mar  9 11:53 ..

root@CONTAINER:/data# echo "Hello John from container-test" >test.txt

root@CONTAINER:/data# ls -al
total 4
drwxr-xr-x. 2 root root 22 Mar  9 12:09 .
drwxr-xr-x. 1 root root 18 Mar  9 11:53 ..
-rw-r--r--. 1 root root 31 Mar  9 12:09 test.txt

root@CONTAINER:/data# cat test.txt
Hello John from container-test

root@CONTAINER:/data# exit
exit

$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                          PORTS               NAMES
97ddd3ba888f        debian              "/bin/bash"         18 minutes ago      Exited (0) About a minute ago                       container-test

$ docker rm $(docker ps -aq)
97ddd3ba888f

$ cd /var/lib

$ ls -l
total 16
drwxr-xr-x.  4 root           root             32 Oct 30 15:48 AccountsService
drwxr-xr-x.  2 root           root             26 Mar  8 08:33 alsa
drwxr-xr-x.  2 root           root           4096 Mar  7 12:24 alternatives
drwx------.  3 root           root             18 Nov  5  2017 authconfig
drwxr-xr-x.  2 root           root              6 Sep 12  2017 bluetooth
drwxr-xr-x.  2 root           root              6 Nov  2 10:49 boltd
drwxr-xr-x.  2 chrony         chrony            6 Apr 12  2018 chrony
drwxr-xr-x.  3 root           root             17 Nov  5  2017 color
drwxr-xr-x.  4 colord         colord           67 Nov  5  2017 colord
drwx------. 10 root           root           4096 Feb 22 09:45 containerd
drwxr-xr-x.  2 root           root              6 Aug  4  2017 cs
drwxr-xr-x.  2 root           root              6 Nov  2 11:19 dbus
drwxr-xr-x.  2 root           root              6 May 15  2018 dhclient
drwxr-xr-x.  2 root           root              6 Oct 30 12:05 dnsmasq
drwx--x--x. 15 root           root            200 Mar  8 08:35 docker	<====
drwxr-xr-x.  2 root           root             85 Mar  7 12:24 docker-engine
drwxr-xr-x.  3 root           root             34 Feb 19 11:39 flatpak
drwxr-xr-x.  2 root           root              6 Oct 30 19:31 fprint
drwxr-xr-x.  3 root           root             39 Feb 21 08:57 fwupd
drwxr-xr-x.  2 root           root              6 Nov  8 18:46 fwupdate
drwxr-xr-x.  2 root           root              6 Apr 10  2018 games
drwxrwx--T.  7 gdm            gdm              97 Mar  8 08:28 gdm
drwxr-xr-x.  2 geoclue        geoclue           6 Oct 30 12:49 geoclue
drwxr-xr-x.  4 root           root             55 Mar  8 08:27 gssproxy
drwxr-xr-x.  2 root           root              6 Oct 30 14:19 hyperv
drwxr-xr-x.  2 root           root              6 Nov  2 12:40 initramfs
drwxr-xr-x.  8 root           root             90 Oct 30 18:10 iscsi
drwxr-xr-x.  9 root           root            106 Feb 12 12:15 libvirt
drwxr-xr-x.  2 root           root              6 Nov  5  2016 lldpad
drwxr-xr-x.  2 root           root             30 Mar  9 03:20 logrotate
drwx------.  2 root           root              6 Nov  5  2017 machines
drwxr-xr-x.  2 root           root             37 Apr 10  2018 misc
drwxr-x---.  2 root           slocate          24 Mar  9 03:20 mlocate
drwxr-xr-x.  4 root           root             45 Oct 30 18:52 net-snmp
drwx------.  2 root           root           4096 Mar  8 08:35 NetworkManager
drwxr-xr-x.  5 root           root            105 Feb 12 12:15 nfs
drwxr-xr-x.  2 root           root              6 Nov  5  2016 os-prober
drwxr-xr-x.  2 root           root             83 Mar  9 06:11 PackageKit
drwxr-xr-x.  7 root           root             71 Nov 19 10:26 pcp
drwxr-xr-x.  2 root           root             27 Apr 13  2018 plymouth
drwxr-x---.  3 root           polkitd          28 Nov  5  2017 polkit-1
drwx------.  2 postfix        root             25 Oct 30 11:50 postfix
drwx------.  2 pulse          pulse             6 Apr 12  2018 pulse
drwxr-xr-x.  2 root           root              6 May 16  2018 rasdaemon
drwx------.  2 rpc            rpc               6 Oct 30 18:00 rpcbind
drwxr-xr-x.  2 root           root           4096 Mar  8 08:28 rpm
drwxr-xr-x.  3 root           root             19 Mar  7 12:23 rpm-state
drwx------.  2 root           root             29 Mar  9 06:20 rsyslog
drwxr-xr-x.  4 root           root             53 Oct 30 17:32 samba
drwxr-xr-x.  2 root           root              6 Jan 29 11:46 selinux
drwx------.  2 setroubleshoot setroubleshoot   71 Nov  2 11:12 setroubleshoot
drwxr-xr-x.  4 root           root             35 Oct 30 17:57 stateless
drwxr-xr-x.  5 root           root             70 Feb 19 11:35 systemd
drwx------.  2 tss            tss               6 Aug  3  2017 tpm
drwxr-xr-x.  2 root           root              6 Nov  4 19:23 tuned
drwx------.  2 root           root              6 Nov  2 15:49 udisks2
drwxr-xr-x.  2 unbound        unbound          22 Jul 15  2018 unbound
drwxr-xr-x.  2 root           root              6 Oct 30 13:12 upower
drwxr-xr-x.  2 root           root             29 Mar  8 08:28 xkb
drwxr-xr-x.  6 root           root             80 Mar  7 13:33 yum

$ su
Password: 

[root@ceph1 lib]# cd docker

[root@ceph1 docker]# ls -l
total 12
drwx------.  2 root root   24 Feb 10  2018 builder
drwx------.  4 root root   92 Feb 22 09:45 buildkit
drwx--x--x.  3 root root   20 Feb 10  2018 containerd
drwx------.  2 root root    6 Mar  9 06:13 containers
drwx------.  3 root root   22 Feb 10  2018 image
drwxr-x---.  3 root root   19 Feb 10  2018 network
drwx------. 19 root root 4096 Mar  9 06:13 overlay2
drwx------.  4 root root   32 Feb 10  2018 plugins
drwx------.  2 root root    6 Mar  8 08:35 runtimes
drwx------.  2 root root    6 Feb 10  2018 swarm
drwx------.  2 root root    6 Mar  9 05:36 tmp
drwx------.  2 root root    6 Feb 10  2018 trust
drwx------. 18 root root 4096 Mar  9 05:53 volumes  <====

[root@ceph1 docker]# cd volumes

[root@ceph1 volumes]# ls -l
total 32
drwxr-xr-x. 3 root root    19 Mar  9 05:43 130b81556fd5116430850867caeaec76dec7334cbdfdb280e7963c47982c9f18
drwxr-xr-x. 3 root root    19 Feb 26 10:58 1d2afc6b7a162f38a400f9ea1224641f0c62d48162bc6490961005e3418673f5
drwxr-xr-x. 3 root root    19 Mar  8 09:16 2f9e4eb4735517798a3e77879c86576e49fb687d2d0605d45a824833cf4f7149
drwxr-xr-x. 3 root root    19 Feb 26 10:43 398b87e16707ec126e38cbaf130fa52a91e374b6a52ea93be3f703582837b7be
drwxr-xr-x. 3 root root    19 Mar  8 11:02 4a233205d25995a098540e408b8e345500f9bcd48407d1277b4546737256e2a2
drwxr-xr-x. 3 root root    19 Mar  9 05:53 5707da3b53603494bd23b437ff21dfba52803af64970ba1c03e6671be9c8acd9 <====
drwxr-xr-x. 3 root root    19 Feb 26 10:20 647f5cd6f94cbfa013b7b6dad7c48b49b3f4e055c4b896266149c2f62050ef52
drwxr-xr-x. 3 root root    19 Mar  8 10:59 715a6d2876bb708ebf68646210620d09d133e011f5980bd006697cfc04ddd52c
drwxr-xr-x. 3 root root    19 Feb 26 14:11 719643b55ae955c29921e9c84ac43ebfb91b1721bfa8d2973b91100d75802395
drwxr-xr-x. 3 root root    19 Mar  8 09:03 854f2ed62e46ae17fdcb15f7b308c8f6c6b5e83d459e1fcfd4332d03308caf0f
drwxr-xr-x. 3 root root    19 Feb 26 10:58 932df3f01c4acbe2db7fbbcfb92cfd47f944870fd8d95998c4d202a51a76dc1d
drwxr-xr-x. 3 root root    19 Feb 26 10:19 ba44b30e69397812cd3e6aff129229e12db45405d33f0e1df73797440071a6f3
drwxr-xr-x. 3 root root    19 Mar  8 09:11 e9aed7880722e93fb1bf7845cef871c9faf495575e55d6b404ff23ebe3563393
drwxr-xr-x. 3 root root    19 Feb 26 09:40 fd74a521be263cd7b8712cab209428a7e6aac62f6194c3c6a0597e963f0d9397
drwxr-xr-x. 3 root root    19 Feb 26 11:55 fe52b21065cd8cca703c34501df56b3291e9c97f8e6f39af6b7fe7163bfede9c
drwxr-xr-x. 3 root root    19 Feb 26 10:21 ffe2266ca4a1545b1da5a5bcecb320b18e8ff28c3d58594fa1197c311c293b26
-rw-------. 1 root root 65536 Mar  9 05:53 metadata.db

[root@ceph1 volumes]# cd 5707da3b53603494bd23b437ff21dfba52803af64970ba1c03e6671be9c8acd9/

[root@ceph1 5707da3b53603494bd23b437ff21dfba52803af64970ba1c03e6671be9c8acd9]# ls -l
total 0
drwxr-xr-x. 2 root root 22 Mar  9 06:09 _data

[root@ceph1 5707da3b53603494bd23b437ff21dfba52803af64970ba1c03e6671be9c8acd9]# cd _data

[root@ceph1 _data]# ls -l
total 4
-rw-r--r--. 1 root root 31 Mar  9 06:09 test.txt

[root@ceph1 _data]# cat test.txt
Hello John from container-test

We take a look at the images in our local repository. We use the Debian image to start a container. Note that we specified the –v flag and defined a /data folder / directory. From the container shell we issue the ls /data command. Nothing is displayed.

Back on our host we issue the “docker inspect” command. Of interest is the section regarding mounted volumes. It tells us that our container /data folder has been mapped to the /var/lib/docker/volumes/5707da3b53603494bd23b437ff21dfba52803af64970ba1c03e6671be9c8acd9/_data path.

Back in our container we cd to data and issue an ls command. All looks normal. We then create a test.txt file with some text. We verify that the file has been created and contains the specified text. We exit the shell and it causes the container to exit.

We then verify that the container exited. We remove the stopped container.

Back in our host computer, we go to the /var/lib directory which was specified by the previous “docker inspect” command. We continue to dive into the path. Finally we reach the test.txt file and display its contents. It matches the contents of the file we created in the container which has now been stopped and deleted.

# **** working with Docker volumes: second approach ****
$ cd

$ cd Docker

$ mkdir docvol

$ cd docvol

$ gvim Dockerfile
:::: :::: ::::

$ cat Dockerfile 
FROM debian:wheezy
VOLUME /data

$ pwd    
/home/johncanessa/Docker/docvol

$ ls -l
total 4
-rw-r--r--. 1 johncanessa johncanessa 32 Mar  9 06:29 Dockerfile

$ docker build -t docvol .    
Sending build context to Docker daemon  2.048kB
Step 1/2 : FROM debian:wheezy
wheezy: Pulling from library/debian
2b15b7abe8b3: Pull complete 
Digest: sha256:2259b099d947443e44bbd1c94967c785361af8fd22df48a08a3942e2d5630849
Status: Downloaded newer image for debian:wheezy
 ---> 10fcec6d95c4
Step 2/2 : VOLUME /data
 ---> Running in b2a9d60dac4f
Removing intermediate container b2a9d60dac4f
 ---> 5e3aded3df8a
Successfully built 5e3aded3df8a
Successfully tagged docvol:latest

$ docker images -a
REPOSITORY           TAG                 IMAGE ID            CREATED             SIZE
docvol               latest              5e3aded3df8a        10 seconds ago      88.3MB	<==== flatten test aa9ddc7ae6db 19 hours ago 91.6MB commit test d1c21fa17ef2 20 hours ago 95MB nginx latest 881bd08c0b08 4 days ago 109MB redis latest 0f88f9be5839 4 days ago 95MB debian wheezy 10fcec6d95c4 4 days ago 88.3MB johncanessa/cowsay latest 30d74744acb1 11 days ago 131MB busybox latest d8233ab899d4 3 weeks ago 1.2MB debian latest d508d16c64cd 4 weeks ago 101MB centos latest 1e1148e4cc2c 3 months ago 202MB $ docker run -it -h CONTAINER docvol /bin/bash root@CONTAINER:/# root@CONTAINER:/# cd root@CONTAINER:~# pwd /root root@CONTAINER:~# cd /data root@CONTAINER:/data# ls -l total 0 root@CONTAINER:/data# echo "in folder data of CONTAINER ..." >testing.txt

root@CONTAINER:/data# ls -l
total 4
-rw-r--r--. 1 root root 32 Mar  9 12:52 testing.txt

root@CONTAINER:/data# cat testing.txt
in folder data of CONTAINER ...

root@CONTAINER:/data# exit
exit

$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
5dff233401b2        docvol              "/bin/bash"         6 minutes ago       Exited (0) 9 seconds ago                       friendly_heisenberg

$ docker rm $(docker ps -aq)
5dff233401b2

$ docker ps -a              
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

$ su
Password: 

[root@ceph1 johncanessa]# cd /var

[root@ceph1 var]# ls -l
total 12
drwxr-xr-x.  2 root root   19 Nov  5  2017 account
drwxr-xr-x.  2 root root    6 Apr 10  2018 adm
drwxr-xr-x. 13 root root  157 Feb 12 12:30 cache
drwxr-xr-x.  2 root root    6 Nov  4 11:10 crash
drwxr-xr-x.  3 root root   34 Feb 12 12:08 db
drwxr-xr-x.  3 root root   18 Apr 10  2018 empty
drwxr-xr-x.  2 root root    6 Apr 10  2018 games
drwxr-xr-x.  2 root root    6 Apr 10  2018 gopher
drwxr-xr-x.  3 root root   18 Jan 29 11:32 kerberos
drwxr-xr-x. 62 root root 4096 Feb 22 09:45 lib  <==== drwxr-xr-x. 2 root root 6 Apr 10 2018 local lrwxrwxrwx. 1 root root 11 Nov 5 2017 lock -> ../run/lock
drwxr-xr-x. 19 root root 4096 Mar  8 09:17 log
lrwxrwxrwx.  1 root root   10 Jul 15  2018 mail -> spool/mail
drwxr-xr-x.  2 root root    6 Apr 10  2018 nis
drwxr-xr-x.  2 root root    6 Apr 10  2018 opt
drwxr-xr-x.  2 root root    6 Apr 10  2018 preserve
lrwxrwxrwx.  1 root root    6 Nov  5  2017 run -> ../run
drwxr-xr-x. 12 root root  140 Apr 10  2018 spool
drwxr-xr-x.  4 root root   28 Nov  2 11:12 target
drwxrwxrwt.  9 root root 4096 Mar  9 06:56 tmp
drwxr-xr-x.  2 root root    6 Apr 10  2018 yp

[root@ceph1 var]# cd lib

[root@ceph1 lib]# cd docker

[root@ceph1 docker]# ls -l
total 12
drwx------.  2 root root   24 Feb 10  2018 builder
drwx------.  4 root root   92 Feb 22 09:45 buildkit
drwx--x--x.  3 root root   20 Feb 10  2018 containerd
drwx------.  2 root root    6 Mar  9 06:53 containers
drwx------.  3 root root   22 Feb 10  2018 image
drwxr-x---.  3 root root   19 Feb 10  2018 network
drwx------. 20 root root 4096 Mar  9 06:53 overlay2
drwx------.  4 root root   32 Feb 10  2018 plugins
drwx------.  2 root root    6 Mar  8 08:35 runtimes
drwx------.  2 root root    6 Feb 10  2018 swarm
drwx------.  2 root root    6 Mar  9 06:44 tmp
drwx------.  2 root root    6 Feb 10  2018 trust
drwx------. 19 root root 4096 Mar  9 06:47 volumes  <====

[root@ceph1 docker]# cd volumes

[root@ceph1 volumes]# ls -lt
total 32
-rw-------. 1 root root 65536 Mar  9 06:47 metadata.db
drwxr-xr-x. 3 root root    19 Mar  9 06:47 a189a3b578d1ec46eafa586d4493efc7e141df70cb642dc3e539efbed3bfb28b <====
drwxr-xr-x. 3 root root    19 Mar  9 05:53 5707da3b53603494bd23b437ff21dfba52803af64970ba1c03e6671be9c8acd9
drwxr-xr-x. 3 root root    19 Mar  9 05:43 130b81556fd5116430850867caeaec76dec7334cbdfdb280e7963c47982c9f18
drwxr-xr-x. 3 root root    19 Mar  8 11:02 4a233205d25995a098540e408b8e345500f9bcd48407d1277b4546737256e2a2
drwxr-xr-x. 3 root root    19 Mar  8 10:59 715a6d2876bb708ebf68646210620d09d133e011f5980bd006697cfc04ddd52c
drwxr-xr-x. 3 root root    19 Mar  8 09:16 2f9e4eb4735517798a3e77879c86576e49fb687d2d0605d45a824833cf4f7149
drwxr-xr-x. 3 root root    19 Mar  8 09:11 e9aed7880722e93fb1bf7845cef871c9faf495575e55d6b404ff23ebe3563393
drwxr-xr-x. 3 root root    19 Mar  8 09:03 854f2ed62e46ae17fdcb15f7b308c8f6c6b5e83d459e1fcfd4332d03308caf0f
drwxr-xr-x. 3 root root    19 Feb 26 14:11 719643b55ae955c29921e9c84ac43ebfb91b1721bfa8d2973b91100d75802395
drwxr-xr-x. 3 root root    19 Feb 26 11:55 fe52b21065cd8cca703c34501df56b3291e9c97f8e6f39af6b7fe7163bfede9c
drwxr-xr-x. 3 root root    19 Feb 26 10:58 932df3f01c4acbe2db7fbbcfb92cfd47f944870fd8d95998c4d202a51a76dc1d
drwxr-xr-x. 3 root root    19 Feb 26 10:58 1d2afc6b7a162f38a400f9ea1224641f0c62d48162bc6490961005e3418673f5
drwxr-xr-x. 3 root root    19 Feb 26 10:43 398b87e16707ec126e38cbaf130fa52a91e374b6a52ea93be3f703582837b7be
drwxr-xr-x. 3 root root    19 Feb 26 10:21 ffe2266ca4a1545b1da5a5bcecb320b18e8ff28c3d58594fa1197c311c293b26
drwxr-xr-x. 3 root root    19 Feb 26 10:20 647f5cd6f94cbfa013b7b6dad7c48b49b3f4e055c4b896266149c2f62050ef52
drwxr-xr-x. 3 root root    19 Feb 26 10:19 ba44b30e69397812cd3e6aff129229e12db45405d33f0e1df73797440071a6f3
drwxr-xr-x. 3 root root    19 Feb 26 09:40 fd74a521be263cd7b8712cab209428a7e6aac62f6194c3c6a0597e963f0d9397

[root@ceph1 volumes]# cd a189a3b578d1ec46eafa586d4493efc7e141df70cb642dc3e539efbed3bfb28b/

[root@ceph1 a189a3b578d1ec46eafa586d4493efc7e141df70cb642dc3e539efbed3bfb28b]# ls -l
total 0
drwxr-xr-x. 2 root root 25 Mar  9 06:52 _data

[root@ceph1 a189a3b578d1ec46eafa586d4493efc7e141df70cb642dc3e539efbed3bfb28b]# cd _data

[root@ceph1 _data]# ls -l
total 4
-rw-r--r--. 1 root root 32 Mar  9 06:52 testing.txt

[root@ceph1 _data]# cat testing.txt
in folder data of CONTAINER ...

[root@ceph1 _data]# exit
exit

In this example we create a Dockerfile with a volume named /data. We then build an image and start a container. One the container is running we use the shell to go to the /data directory and write some text to the testing.txt file. Verify that all worked well and exit the container.

As before, we remove the container and verify it is gone. We then go to the folder in which Docker mounts volume containers. We look for the youngest volume. Look for the text file we created and display its contents. All worked as expected.

# **** working with Docker volumes: third approach ****
$ cd

$ cd Docker/

$ mkdir data

$ ls -l
total 112
drwxr-xr-x. 2 root        root           22 Feb 26 16:39 backup
drwxr-xr-x. 2 johncanessa johncanessa    24 Mar  7 13:37 busybox
drwxr-xr-x. 2 johncanessa johncanessa    83 Feb 25 15:37 cowsay
drwxr-xr-x. 2 johncanessa johncanessa     6 Mar  9 08:35 data	<==== -rw-r--r--. 1 johncanessa johncanessa 6157 Feb 22 10:22 docker_installation.txt -rw-r--r--. 1 johncanessa johncanessa 45300 Mar 9 07:01 docker_tidbits.txt drwxr-xr-x. 2 johncanessa johncanessa 24 Mar 9 06:29 docvol -rw-r--r--. 1 johncanessa johncanessa 54865 Feb 26 16:51 first_steps.txt $ cd data $ echo "in my file generated on the CentOS system" >text.txt

$ ls -l
total 4
-rw-r--r--. 1 johncanessa johncanessa 42 Mar  9 08:46 text.txt

$ cat text.txt
in my file generated on the CentOS system

$ docker run -it -v /home/johncanessa/Docker/data:/data debian /bin/bash

root@3c6ee7b198a9:/# ls -l
total 8
drwxr-xr-x.   2 root root 4096 Feb  4 00:00 bin
drwxr-xr-x.   2 root root    6 Jan 22 13:47 boot
drwxr-xr-x.   2 1000 1000   22 Mar  9 14:46 data	<====
drwxr-xr-x.   5 root root  360 Mar  9 14:48 dev
drwxr-xr-x.   1 root root   66 Mar  9 14:48 etc
drwxr-xr-x.   2 root root    6 Jan 22 13:47 home
drwxr-xr-x.   8 root root   96 Feb  4 00:00 lib
drwxr-xr-x.   2 root root   34 Feb  4 00:00 lib64
drwxr-xr-x.   2 root root    6 Feb  4 00:00 media
drwxr-xr-x.   2 root root    6 Feb  4 00:00 mnt
drwxr-xr-x.   2 root root    6 Feb  4 00:00 opt
dr-xr-xr-x. 285 root root    0 Mar  9 14:48 proc
drwx------.   2 root root   37 Feb  4 00:00 root
drwxr-xr-x.   3 root root   30 Feb  4 00:00 run
drwxr-xr-x.   2 root root 4096 Feb  4 00:00 sbin
drwxr-xr-x.   2 root root    6 Feb  4 00:00 srv
dr-xr-xr-x.  13 root root    0 Mar  8 14:27 sys
drwxrwxrwt.   2 root root    6 Feb  4 00:00 tmp
drwxr-xr-x.  10 root root  105 Feb  4 00:00 usr
drwxr-xr-x.  11 root root  139 Feb  4 00:00 var

root@3c6ee7b198a9:/# cd data

root@3c6ee7b198a9:/data# ls -l
total 4
-rw-r--r--. 1 1000 1000 42 Mar  9 14:46 text.txt

root@3c6ee7b198a9:/data# cat text.txt
in my file generated on the CentOS system

root@3c6ee7b198a9:/data# exit
exit

$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
3c6ee7b198a9        debian              "/bin/bash"         46 seconds ago      Exited (0) 10 seconds ago                       fervent_galileo

$ docker rm $(docker ps -aq)
3c6ee7b198a9

$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

In this case we go to our local Docker folder and make a data folder. We create a text.txt file with some text. Note that this is done before we run a container. This example illustrates how we refer in our container a file previously created in the host computer.

We then start a container and specify a volume pointing to the folder we just created in our host computer.

Now from the shell in the container we look for the /data folder and find the text.txt file. The contents match what we wrote in the host.

We then exit the container and remove the stopped container.

Hope you enjoyed this post. In this series I am used the edited contents of chapter 4 from the book Using Docker by Adrian Mouat. Please keep in mind that reading is not equivalent to learning. You need to experiment when learning technical material.

Keep on learning, experimenting and developing great software and products.

If you have foments or questions regarding this or any other post in this blog, or if you would like for me to help in a software development project, please leave me a note bellow. Note that the note will remain private unless I approve it. That will not happen with help requests.

John

Follow me on Twitter:  @john_canessa

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.