In this article will get to know about how to build an image using Dockerfile, which is the recommended way of building images.

Build context

root@ubuntu14-04:/mydockers# docker build -t helloworld:0.1 .
Sending build context to Docker daemon 2.048 kB
Sending build context to Docker daemon

Here Build context is the file and directories where the Dockerfile is peresent, is sent to the daemon. For now its only the Dockerfile this time and hence the size is low.

NOTE: It is clear that its the docker daemon and not the client does the build. So when you are using a remote client connected to the remote docker daemon, be sure to note that all the files and the folders are sent to the daemon as part of the build context.

Dockerfile Instruction 1

Step 0 : FROM ubuntu:latest
 ---> 56465e1e45d2 

Highlighted one is image id of the ubuntu image.

Dockerfile Instruction 2

Step 1 : MAINTAINER kartik@test.com
 ---> Running in 41babd159939
 ---> 3d37aaaefbe6
Removing intermediate container 41babd159939
  • 41babd159939 is the container id in which the above command runs
  • 3d37aaaefbe6 is the image created after running and committing the above instruction.
  • Then the container 41babd159939 is removed and thrown away.

Dockerfile Instruction 3

Step 2 : RUN apt-get update
 ---> Running in a6e576a53edc
Get:1 http://archive.ubuntu.com/ubuntu xenial InRelease [247 kB]
Get:2 http://archive.ubuntu.com/ubuntu xenial-updates InRelease [102 kB]
...
...

Fetched 24.6 MB in 1min 36s (255 kB/s)
Reading package lists...
 ---> 17b75b23d0ae
Removing intermediate container a6e576a53edc
  • a6e576a53edc is the container id in which the above command runs
  • 17b75b23d0ae is the image created after running and committing the above instruction.
  • Then the container a6e576a53edc is removed and thrown away.

Dockerfile Instruction 4

Step 3 : CMD echo Hello world
 ---> Running in 2cfe3cd356f5
 ---> e2639fe5e21b
Removing intermediate container 2cfe3cd356f5

Here too its the same steps with different image id and container id.

Successfully built image

Successfully built e2639fe5e21b

Once all instructions are run and committed a new image is built e2639fe5e21b.

Checking images list

root@ubuntu14-04:/mydockers# docker images

REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
helloworld          0.1                 e2639fe5e21b        19 minutes ago      166.5 MB
ubuntu              16.10               241684c864ba        8 days ago          101.4 MB
ubuntu              latest              56465e1e45d2        6 weeks ago         127.2 MB

This shows that the image is created with corresponding tag and version.

Now try running the container

root@ubuntu14-04:/mydockers# docker run helloworld:0.1
Hello world

Now try running you will see this output.

Verify in images tree

root@ubuntu14-04:/mydockers# docker images --tree
Warning: '--tree' is deprecated, it will be removed soon. See usage.
├─b229318df569 Virtual Size: 101.4 MB
│ └─5e589e102cd7 Virtual Size: 101.4 MB
│   └─0d56561475b7 Virtual Size: 101.4 MB
│     └─bc872ff91bfd Virtual Size: 101.4 MB
│       └─8d6ef9012092 Virtual Size: 101.4 MB
│         └─241684c864ba Virtual Size: 101.4 MB Tags: ubuntu:16.10
└─58488e45273c Virtual Size: 127.2 MB
  └─25810b66099e Virtual Size: 127.2 MB
    └─6571ba684f54 Virtual Size: 127.2 MB
      └─6ed49a73d8f0 Virtual Size: 127.2 MB
        └─c53777cbfc31 Virtual Size: 127.2 MB
          └─56465e1e45d2 Virtual Size: 127.2 MB Tags: ubuntu:latest
            └─3d37aaaefbe6 Virtual Size: 127.2 MB
              └─17b75b23d0ae Virtual Size: 166.5 MB
                └─e2639fe5e21b Virtual Size: 166.5 MB Tags: helloworld:0.1

All those images id which are above will definitely match the ones highlighted. See all image id’s in the order the instructions were ran.