Search code examples
dockerdocker-composemountdocker-volume

Volume mount one file or directory straight from Container A to Container B


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!


Solution

  • 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.