The undesirable effect of data volume

As mentioned earlier, Docker enables access for us to each data volume in a Docker image using the VOLUME instruction during the build time. Nevertheless, data volumes should never be used to store any data during the build time, otherwise it will result in an unwanted effect.

In this section, we will demonstrate the undesirable effect of using data volume during the build by crafting a Dockerfile, and then showcase the implication by building this Dockerfile.

The following are the details of Dockerfile:

  1. Build the image using Ubuntu 16.04 as the base image:
      # Use Ubuntu as the base image 
FROM ubuntu:16.04
  1. Create a /MountPointDemo data volume using the VOLUME instruction:
      VOLUME /MountPointDemo 
  1. Create a file in the /MountPointDemo data volume using the RUN instruction:
      RUN date > /MountPointDemo/date.txt 
  1. Display the file in the /MountPointDemo data volume using the RUN instruction:
      RUN cat /MountPointDemo/date.txt 
  1. Proceed to build an image from this Dockerfile using the docker build subcommand, as shown here:
      $ sudo docker build -t testvol .
Sending build context to Docker daemon 2.56 kB
Sending build context to Docker daemon
Step 0 : FROM ubuntu:16.04
---> 9bd07e480c5b
Step 1 : VOLUME /MountPointDemo
---> Using cache
---> e8b1799d4969
Step 2 : RUN date > /MountPointDemo/date.txt
---> Using cache
---> 8267e251a984
Step 3 : RUN cat /MountPointDemo/date.txt
---> Running in a3e40444de2e
cat: /MountPointDemo/date.txt: No such file or directory
2014/12/07 11:32:36 The command [/bin/sh -c cat
/MountPointDemo/date.txt] returned a non-zero code: 1

In the preceding output of the docker build subcommand, you would have noticed that the build fails in step 3 because it could not find the file created in step 2. Apparently, the file that was created in step 2 vanishes when it reaches step 3. This undesirable effect is due to the approach Docker uses to build its images. An understanding of the Docker image-building process will unravel the mystery.

In the build process, for every instruction in a Dockerfile, the following steps are followed:

  1. Create a new container by translating the Dockerfile instruction to an equivalent docker run subcommand.
  2. Commit the newly-created container to an image.
  3. Repeat steps 1 and 2 by treating the newly-created image as the base image for step 1.

When a container is committed, it saves the filesystem of the container and deliberately does not save the filesystem of the data volumes. Therefore, any data stored in the data volume will be lost in this process. So, never use a data volume as a storage during the build process.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset