Docker – Sharing data between containers with data volume containers

How to Create a Droplet with DigitalOcean

There is another feature of Docker volumes that allows you to share the volume(s) mounted in one Docker container with other containers. It is called data volume containers. Using data volume containers is basically a two-step process. In the first step, you run a container that either creates or mounts Docker volumes (or both), and in the second step, you use the special volume parameter, --volumes-from, when running other containers to configure them to mount all of the volumes mounted in the first container. Here is an example:

# Step 1
docker container run \
   --rm -d \
   -v data-vol-01:/data/vol1 -v data-vol-02:/data/vol2 \
   --name data-container \
   vol-demo2:1.0 tail -f /dev/null
# Step 2
docker container run \
   --rm -d \
   --volumes-from data-container \
   --name app-container \
   vol-demo2:1.0 tail -f /dev/null
# Prove it
docker container exec app-container ls -l /data
# Prove it more
docker container inspect -f '{{ range .Mounts }}{{ .Name }} {{ end }}' app-container

Here is what that looks like when executed:

In this example, the first container run command is creating the volumes, but they could have just as easily been pre-created with an earlier container run command, or from a volume create command.

Comments are closed.