Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Relative Path with Docker-Compose Shared Volumes

It seems this question has already been asked by another poster, but there was no answer. I have more information on the matter, so I'm re-asking the same question - but with much additional material.

I'm running Docker on Windows 10. I have Docker configured for using Linux containers (not Windows containers). I have my C drive configured as a shared drive.

The Problem

I have the following docker-compose file called wp.yml:

version: '3.2'

services:
   db:
     image: mysql:5.7
     volumes:
       - type: volume
         source: wp_dbA
         target: /var/lib/mysql       
     restart: always

volumes:
    wp_dbA:
      driver_opts:
        type: none
        device: ./wp-db
        o: bind

I execute as follows:

c:\repos\wpsand\wpA> docker-compose -f wp.yml up -d

I receive the following error:

ERROR: for wpa_db_1 Cannot create container for service db: error while mounting volume with options: type='none' device='./wp-db' o='bind': no such file or directory

ERROR: for db Cannot create container for service db: error while mounting volume with options: type='none' device='./wp-db' o='bind': no such file or directory ERROR: Encountered errors while bringing up the project.

Despite the message,the folder wp-db does already exist, located here:

c:/repos/wpsand/wpA/wp-db

Clues...

Despite the above error, checking docker volume ls reveals that volume wpa_wp_dbA was freshly created.

Clue #1

If I change the shared volume device to use an absolute path (such as /c/repos/wpsand/wpA), then run again...I get the same problem. However, the error message still says that device with relative path ./wp-db cannot be found.

Apparently the re-use of the shared volume named wp_dbA causes any newly specified path to be ignored. The original ./wp-db is what it will attempt to use.

Clue #2

If I revert back to a relative path, but change the name of the shared volume to "wp_dbB", I still get the same error.

Clue #3

This combines #1 and #2 above. If I invent a new shared volume name, and use an absolute path, it works! My container is launched successfully. Unfortunately, I'm no longer using the relative path I wanted.

Clue #4

Now that my container works, I docker-compose down and change the docker-compose file again. I put the relative path back in, and "compose up". It works!

Conclusion?

It looks like relative paths simply don't work. But I've been seeing docker-compose files posted online using this same shared volume relative path notation. Perhaps it works for Docker, but not Docker on Windows?

Can anyone suggest a work-around?

Update!

I found this SO post that very nearly has a relative-path work-around. I updated my compose file to use the ${PWD} notation, and launched it from my Git Bash shell. Now I got this error:

$ docker-compose up -d
Creating wpa_db_1        ... error
Creating wpa_db_1        ...

ERROR: for wpa_db_1  Cannot create container for service db: error 
while mounting volume with options: type='none'
device='C:/repos/wpsand/wpA/wp-db' o='bind': no such file or directory

ERROR: for db  Cannot create container for service db: error while 
mounting volume with options: type='none' device='C:/repos/wpsand/wpA/wp-db'
o='bind': no such file or directory
Encountered errors while bringing up the project.

That almost worked! If only the ${PWD} had expanded into a Linux mount notation (/c/repos/wpsand/wpA/wp-db)!

like image 307
Brent Arias Avatar asked Feb 27 '18 20:02

Brent Arias


People also ask

Can docker volumes be shared?

You can manage volumes using Docker CLI commands or the Docker API. Volumes work on both Linux and Windows containers. Volumes can be more safely shared among multiple containers. Volume drivers let you store volumes on remote hosts or cloud providers, to encrypt the contents of volumes, or to add other functionality.

Can two docker containers share the same volume?

Multiple containers can run with the same volume when they need access to shared data. Docker creates a local volume by default. However, we can use a volume diver to share data across multiple machines. Finally, Docker also has –volumes-from to link volumes between running containers.

Does Docker compose create volume directories?

Running docker-compose up results in creation of directories specified via volumes directive on the host system.

Where do I mount docker volumes?

Volumes are stored in a part of the host filesystem which is managed by Docker ( /var/lib/docker/volumes/ on Linux).


1 Answers

./ syntax worked for me for win 10 environment.

volumes:
  - ./mdata/:/data/db/
  - ./logs/:/var/log/mongodb/mongod.log
  - ./mongod.cfg:/etc/mongod.conf.orig
like image 53
Davut Gürbüz Avatar answered Sep 29 '22 21:09

Davut Gürbüz