问题
I've already tried the solution in this stackoverflow thread. It did not work for me, as shown below.
Here's my docker-compose.yml file:
version: "3.7"
services:
db:
image: mysql
volumes:
- data:/var/lib/mysql
volumes:
data:
driver_opts:
type: none
device: /usr/local/opt/mysql/mywebsite.com
o: bind
Here's the result of docker-compose up:
$ docker-compose up
Creating volume "mycontainer_data" with default driver
Recreating 45bc03b2c674_mycontainer_db_1 ... error
ERROR: for 45bc03b2c674_mycontainer_db_1 Cannot create container for service db: error while mounting volume with options: type='none' device='/usr/local/opt/mysql/mywebsite.com' 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='/usr/local/opt/mysql/mywebsite.com' o='bind': no such file or directory
ERROR: Encountered errors while bringing up the project.
Instead of type: none, I have also tried type: volume and type: bind, but I got the same error.
The directory clearly exists:
$ ls -al /usr/local/opt/mysql/
...
drwxr-xr-x 2 cameronhudson staff 64 Jun 10 10:32 mywebsite.com
...
回答1:
There were 2 reasons why my configuration wasn't working.
First, Docker does not follow symlinks, and the mysql directory is a symlink:
$ ls -al /usr/local/opt | grep mysql
lrwxr-xr-x 1 cameronhudson admin 22 Jan 23 17:42 mysql -> ../Cellar/mysql/8.0.13
However, even after using a path without any symlinks, /databases/mywebsite.com, I continued the experience the same no such file or directory error.
I found that if I changed my docker-compose.yml file to mount this directory as a nameless volume, I got a different, more reasonable error:
version: "3.7"
services:
db:
image: mysql
volumes:
- /databases/mywebsite.com:/var/lib/mysql
The new error:
ERROR: for mycontainer_db_1 Cannot start service db: b'Mounts denied: \r\nThe path /databases/mywebsite.com\r\nis not shared from OS X and is not known to Docker.\r\nYou can configure shared paths from Docker -> Preferences... -> File Sharing.\r\nSee https://docs.docker.com/docker-for-mac/osxfs/#namespaces for more info.\r\n.'
After I added this path in Docker Desktop, I was able to up the services using my original configuration. It also worked with type: none, type: volume, type: bind, or leaving type out altogether.
来源:https://stackoverflow.com/questions/56531493/how-can-i-mount-an-absolute-host-path-as-a-named-volume-in-docker-compose