Fredrik
Fredrik

Reputation: 449

Docker compose problem with volume container attachment

I have a docker image "doc_image" and a docker volume "doc_volume". I want to spin up a container from the image where the volume is mounted into a specific point

If I do this with docker run like this:

docker run -d -p 5000:5000 -v doc_volume:/directory doc_image

then it runs flawlessly (I can see the expected files in /directory in interactive way). However, when I try to spin it up with docker-compose like with a docker-compose.yml like this:

version '3'
services:
   my_service:
      image: doc_image
      volumes:
         - doc_volume:/directory
volumes:
   doc_volume:

there is nothing in /directory:

FileNotFoundError: [Errno 2] No such file or directory: '/directory/file.txt'

What went wrong here?

Upvotes: 0

Views: 158

Answers (1)

grapes
grapes

Reputation: 8646

Add external property to volumes section:

version '3'
services:
   my_service:
      image: doc_image
      volumes:
         - doc_volume:/directory
volumes:
   doc_volume:
       external: true   # << here we go

Your problem is that docker-compose creates another volume unless you explicitly tell him to use external one. External means creates not by means of docker-compose.

Upvotes: 1

Related Questions