Reputation: 1003
I want to know if there is a way to volume mount a directory from CONTAINER-A(collector) to CONTAINER-B(store) straight within the same docker network?
I want to do this through the docker-compose
file
Currently I have the following:
---
services:
collector:
hostname: "collector"
container_name: "collector"
volumes_from:
- container:store
store:
hostname: "store"
container_name: "store"
version: "2.1"
I have a directory in docker container (collector) with some files: /home/collector/data
Which I would like to be volume mounted to a directory somewhere within the docker container: store
Any way to make sure we mount it directory without copying it to somewhere else in the middle first.
I have tried the following but to no avail:
-volumes
-volumes_from
Would appreciate your help, thanks!
Upvotes: 0
Views: 297
Reputation: 263617
The only time volumes ever copy data is when they are a named volume being initialized from the image. Beyond that, a volume is always a direct filesystem mount to the volume source. The data in a volume does not exist inside of a container or image, it is only mounted there.
To mount the same volume in two different containers, it's recommended to use a named volume, and point both containers to the same volume name:
version: "2.1"
volumes:
data:
services:
collector:
image: collector
volumes:
- data:/home/collector/data
store:
image: store
volumes:
- data:/home/collector/data
Note that named volumes are initialized by default, but only once to the contents of the first container to start with the volume.
Also note that file mounts are non-trivial because of the way Linux inodes work. If you are modifying the file and looking for your changes to appear between containers, then try to mount the directory instead of the file.
Upvotes: 1
Reputation: 1907
Probably best to define a volume at the top level, then mount that into each container, something like this:
---
version: "2.1"
volumes:
foo: {}
services:
collector:
hostname: "collector"
container_name: "collector"
volumes:
- foo:/path/inside/collector
store:
hostname: "store"
container_name: "store"
volumes:
- foo:/path/inside/store
This will create you a data volume independent of the two containers. If you recreate the containers, the data will stick around. You can use docker volume <command>
to interact with the volume. Alternatively, a simple way to destroy everything is docker-compose down -v
.
Upvotes: 1