Anton Linevych
Anton Linevych

Reputation: 647

How to select volume mountpoint in docker-compose.yml?

I need to mount volume /path/a from one container to /path/b in another container, but according to documentation only HOST:CONTAINER or HOST:CONTAINER:ro allowed to write in VOLUMES section.

Any ideas how to do that in docker-compose.yml?

Upvotes: 2

Views: 6432

Answers (1)

Antti Kuosmanen
Antti Kuosmanen

Reputation: 840

You could create a volume on the host with a bind mount for both containers.

Example:

mkdir -p /mnt/shared-volume
docker run --name container1 -v /mnt/shared-volume:/path/a mycontainer
docker run --name container2 -v /mnt/shared-volume:/path/b mycontainer

Same with docker-compose.yml:

volumes:
 - /mnt/shared-volume:/path/a

And for the other container:

volumes:
 - /mnt/shared-volume:/path/b

Alternative solution:

Create a data volume container!

Example:

docker run --name datacontainer -v /mnt/shared-volume mycontainer /bin/true
docker run --name container1 --volumes-from datacontainer mycontainer
docker run --name container2 --volumes-from datacontainer mycontainer

Upvotes: 4

Related Questions