Mounting data volume from other containers

The Docker Engine provides a nifty interface to mount (share) the data volume from one container to another. Docker makes this interface available through the --volumes-from option of the docker run subcommand. The --volumes-from option takes a container name or container ID as its input and automatically mounts all the data volumes available on the specified container. Docker allows you to mount multiple containers with data volume using the --volumes-from option multiple times.

Here is a practical example that demonstrates how to mount data volume from another container and showcases the data volume mount step by step:

  1. We begin with launching an interactive Ubuntu container by mounting the data volume from the data-only container (datavol), which we launched in the previous section:
      $ sudo docker run -it 
--volumes-from datavol
ubuntu:latest /bin/bash
  1. Now from the container's prompt, let's verify the data volume mounts using the mount command:
      root@e09979cacec8:/# mount | grep DataMount
/dev/xvda2 on /DataMount type ext3
(rw,noatime,nobarrier,errors=remount-ro,data=ordered)

Here, we successfully mounted the data volume from the datavol data-only container.

  1. Next, we need to inspect the data volume of this container from another Terminal using the docker inspect subcommand:
      $ sudo docker inspect --format='{{json .Mounts}}'   
e09979cacec8

[{"Name":
"7907245e5962ac07b31c6661a4dd9b283722d3e7d0b0fb40a90
43b2f28365021","Source":
"/var/lib/docker/volumes
/7907245e5962ac07b31c6661a4dd9b283722d3e7d0b0fb40a9043b
2f28365021/_data","Destination":"
/DataMount","Driver":"local","Mode":"",
"RW":true,"Propagation":""}]

Evidently, the data volume from the datavol data-only container is mounted as if they were mounted directly on to this container.

We can mount a data volume from another container and also showcase the mount points. We can make the mounted data volume to work by sharing data between containers using the data volume, as demonstrated here:

  1. Let's reuse the container that we launched in the previous example and create a /DataMount/testfile file in the /DataMount data volume by writing some text to the file, as shown here:
      root@e09979cacec8:/# echo 
"Data Sharing between Container" >
/DataMount/testfile
  1. Just spin off a container to display the text that we wrote in the previous step, using the cat command:
      $ sudo docker run --rm 
--volumes-from datavol
busybox:latest cat /DataMount/testfile

The following is the typical output of the preceding command:

      Data Sharing between Container

Evidently, the preceding Data Sharing between Container output of our newly containerized cat command is the text that we have written in /DataMount/testfile of the datavol container in step 1.

Cool, isn't it? You can share data seamlessly between containers by sharing the data volumes. Here, in this example, we used data-only containers as the base container for data sharing. However, Docker allows us to share any type of data volumes and to mount data volumes one after another, as depicted here:

$ sudo docker run --name vol1 --volumes-from datavol 
busybox:latest /bin/true
$ sudo docker run --name vol2 --volumes-from vol1
busybox:latest /bin/true

Here, in the vol1 container, we mounted the data volume from the datavol container. Then, in the vol2 container, we mounted the data volume from the vol1 container, which is eventually from the datavol container.

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

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