Search code examples
dockerdocker-composedocker-volume

Docker-compose volume doesn't work as expected


I'm trying to mount a volume via docker-composer but after running docker-compose up the directory is empty.

Dockerfile

FROM alpine:3.8

COPY test.txt ./app/

docker-compose.yml

version: "3.7"

services:
  test:
    image: myrep/image:latest
    volumes:
      -  "./app/:/app/"

My procedure:

  1. Build docker image on client (docker build .)
  2. Push docker image to my registry (docker tag xxx myrep/image && docker push myrep/image)
  3. On the server I pull the image (docker pull myrep/image)
  4. Run docker-compose up (docker-compose up)
  5. Then when I look into the app folder there is no test.txt file

Any idea what I'm doing wrong?


Solution

  • You copied the file into the image, but when you start the container you overwrite it with your directory when mounting it.

    If you want the file to be there don’t mount the volume.

    You can verify this by running the image without a volume and executing:

    docker-compose exec test ls -l /app