What happens when you type docker run centos /bin/echo hello world the first time?
Docker could not find the required image locally, so Docker downloaded the required files. Wait a moment… What required files and why plural?
Unable to find image 'centos' locally Pulling repository centos ac1f1b8dce20: Download complete a5fd0258f31c: Download complete d5844d902074: Download complete 511136ea3c5a: Download complete 0b2c58b208cf: Download complete hello world
The basic element in Docker are images. These are basically prebuilt operating systems, often with preinstalled applications. A good overview can be found at Docker’s github page.
But why would Docker download five images?
Lets have a look at the installed images:
REPOSITORY TAG IMAGE ID centos centos5 d5844d902074 centos centos6 ac1f1b8dce20 centos centos7 a5fd0258f31c centos latest a5fd0258f31c
Looking at the IMAGE ID column we identify three of our downloads. Tags centos7 and latest share the same id.
To find the missing ones we have to issue the images command with the tree parameter.
docker images -t
└─511136ea3c5a Virtual Size: 0 B └─0b2c58b208cf Virtual Size: 0 B ├─d5844d902074 Virtual Size: 467.1 MB Tags: centos:centos5 ├─ac1f1b8dce20 Virtual Size: 212.7 MB Tags: centos:centos6 └─a5fd0258f31c Virtual Size: 224 MB Tags: centos:centos7, centos:latest
Found them! Found what?
Docker never forgets
A Docker image is built on layers. With every command a new layer is created. You can even go back to a previous layer. And that is how the tree structure is created and one of the largest benefits of docker is made.
Let’s play with these layers. First run a new command to modify the existing centos container:
docker run centos touch /test
With docker list command we can get the Container ID (-a is used for non-running containers):
docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS NAMES 1ced8cf536d2 centos:centos7 touch /test 2 seconds ago Exit 0 hungry_heisenberg
We use the generated ID to commit a new layer:
docker commit 1ced8cf536d2 centos:touch_test
It created a new TAG named touch_test for the centos Repository.
docker images -t
└─511136ea3c5a Virtual Size: 0 B └─0b2c58b208cf Virtual Size: 0 B ├─d5844d902074 Virtual Size: 467.1 MB Tags: centos:centos5 ├─ac1f1b8dce20 Virtual Size: 212.7 MB Tags: centos:centos6 └─a5fd0258f31c Virtual Size: 224 MB Tags: centos:centos7, centos:latest └─ec04dda9725a Virtual Size: 224 MB Tags: centos:touch_test
Looking inside we see a test file in the root folder:
docker run centos:touch_test ls /
To be continued