Search code examples
dockerdocker-composedockerfiledocker-volume

docker-compose shared volumes between service


I would like to shared files from one service to an other. I make a short exemple.

  • Docker-compose.yml
  • mobile:
    • dockerfile
    • app.apk
  • web:
    • dockerfile

docker-compose.yml

version: "3.7"

services:
  web:
    build: ./web
    depends_on: 
      - mobile
    volumes:
      - data-volume:/var/lib/shared

  mobile:
    build: ./mobile
    volumes:
      - data-volume:/var/lib/shared

volumes:
  data-volume:

Dockerfile.yml (mobile)

FROM node:alpine AS mobile

COPY app.apk /var/lib/shared

RUN ls /var/lib/shared

Dockerfile.yml (web)

FROM node:alpine AS web

RUN ls /var/lib/shared

So I want to see app.apk from mobile service in web service.

I try to use volumes in docker-compose but it's not working. I have tried to figure out for hours.

Someone know how to do that ?


Solution

  • the dockerfile is executed first. The compose-file then overrides the shared folder. You can't access volumes in dockerfiles.

    • When you first built the docker-container, your app.apk gets copied into the docker-image.
    • Then compose starts the container and mounts the new (and probably empty) folder to /var/lib/shared.
    • Then the old content of /var/lib/shared is "overshadowed" by the new content and you can't access the old content anymore.

    solution: copy the app into the volume after starting the container.

    using docker exec -it <container_name> bash you can start a shell in the container after it has started. You can find the name with docker ps.