如何在docker-compose中将绝对主机路径安装为命名卷?

时间:2019-06-10 18:12:47

标签: docker-compose

我已经在this stackoverflow thread中尝试了该解决方案。如下所示,它对我不起作用。

这是我的docker-compose.yml文件:

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  

这是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.

我也尝试了type: nonetype: volume而不是type: bind,但是遇到了同样的错误。

该目录显然存在:

$ ls -al /usr/local/opt/mysql/
...
drwxr-xr-x   2 cameronhudson  staff      64 Jun 10 10:32 mywebsite.com
...

1 个答案:

答案 0 :(得分:0)

我的配置不起作用有两个原因。

首先, Docker不遵循符号链接,并且mysql目录是符号链接:

$ ls -al /usr/local/opt | grep mysql
lrwxr-xr-x    1 cameronhudson  admin    22 Jan 23 17:42 mysql -> ../Cellar/mysql/8.0.13

但是,即使在使用没有任何符号链接的路径/databases/mywebsite.com之后,我仍然遇到同样的no such file or directory错误。

我发现,如果我更改了docker-compose.yml文件以将该目录作为无名卷安装,则会出现另一个更合理的错误:

version: "3.7"
services:
  db:
    image: mysql
    volumes:
     - /databases/mywebsite.com:/var/lib/mysql

新错误:

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.'

我在Docker Desktop中添加此路径后,我能够使用原始配置up来服务。它也可以与type: nonetype: volumetype: bind一起使用,或者将type完全排除在外。