Search code examples
docker-composedocker-volume

docker-compose: how to map a file from a container into a volume


I'm trying to share a file from one container to another other. An essential detail is that (the machine running) my docker host does not have explicit access to the file: It pulls a git repo and doesn't know about the internal file organization of the repo (with the single exception of the docker-compose file). Hence, the approach of standard mapping <host-path>:<container-path> is not applicable; e.g. this is not possible for me: How to mount a single file in a volume

Below is the docker-compose file, stripped for increased readability. Say service_1 has the file /app/awesome.txt. We then want to mount it into service_2 as /opt/awesome.txt.

# docker-compose.yml

version: '3'

services:

    service_1:
        volumes:
            - shared_vol:/public
            # how to make service_1 map 'awesome.txt' into /public ?

    service_2:
        volumes:
            - shared_vol/awesome.txt:/opt/awesome.txt

volumes:
    shared_vol:

Working solutions that I have, but are not fond of,

  • running a script/cmd within service_1, copying the file into the shared volume: causes race-condition as service_2 needs the file upon startup
  • introducing a third service, which the other two depends_on, and does nothing but put the file in the shared volume

Any help, tips or guidance is most appreciated!


Solution

  • can you just do something like this

    version: '3.5'
    
    volumes:
       xxx:
    
    services:
    
       service_1:
          ...
          volumes:
             - xxx:/app
    
       service_2:
          ...
          volumes:
             - xxx:/opt