22

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?

Pang
  • 9,564
  • 146
  • 81
  • 122
Ciel
  • 5,551
  • 5
  • 17
  • 24

1 Answers1

45

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!

Stéphane C.
  • 1,801
  • 2
  • 16
  • 12
  • if you still need to use data-only containers, you may also consider creating an entrypoint script in your FTP image that receives the folder name in argument and symlink it as the service root directory. – Stéphane C. May 03 '16 at 10:25
  • 5
    "Just remember, if both containers are using different images, be careful with ownership and permissions!" Could you elaborate on that? – stefanbschneider Aug 02 '19 at 12:30
  • If the second container had some pre-existing stuff under /data directory, what happens then? Both 'hello.txt' and those pre-existing stuff co-exists? Or 'hello.txt' replacing pre-existing stuff? Or else? – Brad Pitt Jun 05 '20 at 00:31
  • @theelee What normally resides in the image directory is entirely "hidden" by the content of a mounted volume. In my example, any pre-existing files under `/data` in the image `debian:jessie` would not be directly accessible from the container instance. If you'd need to mix original and external data, you'd have to mount individual files instead of the entire directory, but if random files are created and updated, that would quickly become unmanageable. – Stéphane C. Jun 16 '20 at 06:13