5

Container #1 runs with a volume defined for /data, and I'd like to use the run option --volumes-from to mount this volume to another container, but I'd like to change the path for the second container.

In other words /data from container #1 should be mounted to /custom/data inside container #2.

Is that possible? Is there a solution for this?

Thanks!

thasmo
  • 9,159
  • 6
  • 27
  • 32
  • 3
    No, it's currently not possible to customize the `--volumes-from`option. You can try [this "dirty" hack][1] though. [1]: http://stackoverflow.com/questions/23137544/how-to-map-volume-paths-using-dockers-volumes-from – mbarthelemy Jul 21 '14 at 17:39
  • 1
    Actually, this was a comment. @mbarthelemy would you mind putting it as an answer that can be accepted? – andi5 Nov 02 '14 at 18:19
  • Does a symlink not do the trick for you? If you dont want to mutate the docker image, you can also consider using an environment variable to pass to a bootstrap script that makes the symlink, or anything like that. – RoyB Jan 07 '15 at 09:51

2 Answers2

2

It is currently not possible to add custom options to --volumes-from.

There are two possible solutions. However, neither is perfect...

Solution 1

Personally, I do not like this as it only works in Linux. It will not work with Boot2Docker. How to map volume paths using Docker's --volumes-from?

Solution 2

The second solution is to use a sym-link. This works pretty great but there is a caveat at the end.

Run a docker container with an interactive shell and the volumes from container #1; busybox is ideal for this:

docker run --rm -it --volumes-from container-1-name busybox

where container-1-name is the name of container#1.

Within the shell that opens, you can create a symbolic link that points from /custom/data to /data. Assuming that the root to /custom already exists (use mkdir first if not), type:

ln -s /data /custom/data

Now you can check this link exists. You should see something like this:

/ # ls -l custom/
total 0
lrwxrwxrwx    1 root     root             5 Mar  4 08:09 data -> /data

Then you are done with this container. Exit. Now, if you create a new container than uses --volumes-from container-1-name, it can access the contents of /data via /custom/data.

Caveat: You should note that containers using --volumes-from container-1-name will have both /data and /custom/data. There is no way around that using this solution. In most situations, I imagine this is fine. However, if this no good for you, you will need to find another solution.

Community
  • 1
  • 1
freethebees
  • 957
  • 10
  • 24
0

Why you want to use --volumes-from? You can use:

docker run -d -v DataVolume1:/path1 --name container1 image1:v1.0.0
docker run -d -v DataVolume1:/path2 --name container2 image2:v1.0.0

or better --mount instead of -v. You can create and then attach the container or create and attach directly the volume to the first container and then to the second (my example)

Davide
  • 75
  • 1
  • 2
  • 11