Data volume

Data volume is the fundamental building block of data sharing in the Docker environment. Before getting into the details of data sharing, it is imperative to get a good understanding of the data volume concept. Until now, all the files that we create in an image or a container is part and parcel of the union filesystem. The container's union filesystem perishes along with the container. In other words, when the container is removed, its filesystem is also automatically removed. However, the enterprise-grade applications must persist data and the container's filesystem will not render itself for such a requirement.

The Docker ecosystem, however, elegantly addresses this issue with the data volume concept. Data volume is essentially a part of the Docker host filesystem and it simply gets mounted inside the container. Optionally, you can use other advanced filesystems such as Flocker and GlusterFS as data volumes through pluggable volume drivers. Since data volume is not a part of the container's filesystem, it has a life cycle independent of the container.

A data volume can be inscribed in a Docker image using the VOLUME instruction of the Dockerfile. Also, it can be prescribed during the launch of a container using the -v option of the docker run subcommand. Here, in the following example, the implication of the VOLUME instruction in the Dockerfile is illustrated in detail in the following steps:

  1. Create a very simple Dockerfile with the instruction of the base image (ubuntu:16.04) and the data volume (/MountPointDemo):
      FROM ubuntu:16.04 
VOLUME /MountPointDemo
  1. Build the image with the mount-point-demo name using the docker build subcommand:
      $ sudo docker build -t mount-point-demo .
  1. Having built the image, let's quickly inspect the image for our data volume using the docker inspect subcommand:
      $ sudo docker inspect mount-point-demo
[
{
"Id": "sha256:<64 bit hex id>",
"RepoTags": [
"mount-point-demo:latest"
],
... TRUNCATED OUTPUT ...
"Volumes": {
"/MountPointDemo": {}
},
... TRUNCATED OUTPUT ...

Evidently, in the preceding output, data volume is inscribed in the image itself.

  1. Now, let's launch an interactive container using the docker run subcommand from the earlier crafted image, as shown in the following command:
      $ sudo docker run --rm -it mount-point-demo

From the container's prompt, let's check the presence of data volume using the ls -ld command:

      root@8d22f73b5b46:/# ls -ld /MountPointDemo
drwxr-xr-x 2 root root 4096 Nov 18 19:22
/MountPointDemo

As mentioned earlier, data volume is part of the Docker host filesystem and it gets mounted, as shown in the following command:

      root@8d22f73b5b46:/# mount | grep MountPointDemo
/dev/xvda2 on /MountPointDemo type ext3
(rw,noatime,nobarrier,errors=remount-ro,data=ordered)
  1. In this section, we inspected the image to find out about the data volume declaration in the image. Now that we have launched the container, let's inspect the container's data volume using the docker inspect subcommand with the container ID as its argument in a different Terminal. We created a few containers previously and for this purpose, let's take the 8d22f73b5b46 container ID directly from the container's prompt:
      $ sudo docker inspect -f  
'{{json .Mounts}}' 8d22f73b5b46
[
{
"Propagation": "",
"RW": true,
"Mode": "",
"Driver": "local",
"Destination": "/MountPointDemo",
"Source":
"/var/lib/docker/volumes
/720e2a2478e70a7cb49ab7385b8be627d4b6ec52e6bb33063e4144355d59592a/_data",
"Name": "720e2a2478e70a7cb49ab7385b8be627d4b6ec52e6bb33063e4144355d59592a"
}
]

Apparently, here, data volume is mapped to a directory in the Docker host, and the directory is mounted in the read-write mode. This directory, also called as volume, is created by the Docker Engine automatically during the launch of the container. Since version 1.9 of Docker, the volumes are managed through a top-level volume management command, which we will dive and dig further down into tell all in the next section.

So far, we have seen the implication of the VOLUME instruction in the Dockerfile, and how Docker manages data volume. Like the VOLUME instruction of the Dockerfile, we can use the -v <container mount point path> option of the docker run subcommand, as shown in the following command:

$ sudo docker run -v /MountPointDemo -it ubuntu:16.04  

Having launched the container, we encourage you to try the ls -ld /MountPointDemo and mount commands in the newly launched container, and then also, inspect the container, as shown in the preceding step 5.

In both the scenarios described here, the Docker Engine automatically creates the volume under the /var/lib/docker/volumes/ directory and mounts it to the container. When a container is removed using the docker rm subcommand, the Docker Engine does not remove the volume that was automatically created during the launch of the container. This behavior is innately designed to preserve the state of the container's application that was stored in the volume filesystem. If you want to remove the volume that was automatically created by the Docker Engine, you can do so while removing the container by providing a -v option to the docker rm subcommand, on an already stopped container:

$ sudo docker rm -v 8d22f73b5b46  

If the container is still running, then you can remove the container as well as the autogenerated directory by adding a -f option to the previous command:

$ sudo docker rm -fv 8d22f73b5b46  

We have taken you through the techniques and tips to autogenerate a directory in the Docker host and mount it to the data volume in the container. However, with the -v option of the docker run subcommand, a user-defined directory can be mounted to the data volume. In such cases, the Docker Engine will not autogenerate any directory.

The system generation of a directory has a caveat of directory leak. In other words, if you forget to delete the system-generated directories, you may face some unwanted issues. For further information, read the Avoiding common pitfalls section in this chapter.
..................Content has been hidden....................

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