Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sharing volume between Docker containers

I am using Docker to deploy some services and I want to share the Docker volumes between different containers.

Suppose I have a Docker container A which mounts a volume at /data. Here is its Dockerfile:

VOLUME /data

From my understanding, this will attach a volume to the container but it will not mount a host directory to the container. So the data inside this volume is still inside the container A.

I have another container B which provides an FTP service. It accesses the data under volume /public. Its Dockerfile is:

VOLUME /public

Now I want to link them together so that I can use B to manage A's data. From the Docker doc https://docs.docker.com/engine/userguide/containers/dockervolumes/ I shall use the --volumes-from flag to mount A's data volume to B. But this command will mount A's data to /data in B instead of /public, and in this case, the container B is not able to access the data. I didn't see any way to rename the mount point.

Any suggestions or best practices to handle this case?


The data-only container gives a good solution for this case. But if you want to use volumes-from and mount the data to different mount point, this question may be helpful! How to map volume paths using Docker's --volumes-from?

like image 826
Ciel Avatar asked May 03 '16 09:05

Ciel


People also ask

Can two docker containers share same volume?

Multiple containers can run with the same volume when they need access to shared data. Docker creates a local volume by default.

Can we share data volume in docker?

You can manage volumes using Docker CLI commands or the Docker API. Volumes work on both Linux and Windows containers. Volumes can be more safely shared among multiple containers. Volume drivers let you store volumes on remote hosts or cloud providers, to encrypt the contents of volumes, or to add other functionality.


1 Answers

You may find a lot of pointers mentioning data-only containers and --volumes-from. However, since docker 1.9, volumes have become first class citizens, they can have names, and have more flexibility:

It's now easy to achieve the behavior you want, here's an example :

  1. Create a named data volume with name service-data:

    docker volume create --name service-data
    
  2. You can then create a container that mounts it in your /public folder by using the -v flag:

    docker run -t -i -v service-data:/public debian:jessie /bin/bash
    

    For testing purpose we create a small text file in our mapped folder:

    cd public
    echo 'hello' > 'hello.txt'
    
  3. You may then attach your named volume to a second container, but this time under the data folder:

    docker run -t -i -v service-data:/data debian:jessie /bin/bash
    ls /data       #-->shows "hello.txt"
    

Just remember, if both containers are using different images, be careful with ownership and permissions!

like image 166
Stéphane C. Avatar answered Oct 07 '22 23:10

Stéphane C.