如何在docker-compose中将绝对主机路径安装为命名卷? [英] How can I mount an absolute host path as a named volume in docker-compose?

查看:227
本文介绍了如何在docker-compose中将绝对主机路径安装为命名卷?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在此stackoverflow线程中尝试了该解决方案。如下所示。

I've already tried the solution in this stackoverflow thread. It did not work for me, as shown below.

这是我的 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

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.

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

Instead of type: none, I have also tried type: volume and type: bind, but I got the same error.

该目录显然存在:

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


推荐答案

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

There were 2 reasons why my configuration wasn't working.

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

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

但是,即使使用了没有任何符号链接的路径, /databases/mywebsite.com ,我继续遇到同样的没有这样的文件或目录错误的经验。

However, even after using a path without any symlinks, /databases/mywebsite.com, I continued the experience the same no such file or directory error.

我发现,如果我更改了 docker-compose.yml 文件以将该目录挂载为无名卷,我会得到一个不同的结果,更合理的错误:

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

新错误:

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中添加此路径后,我能够 c $ c>使用我的原始配置的服务。它还适用于类型:无类型:音量类型:绑定,或完全省略 type

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.

这篇关于如何在docker-compose中将绝对主机路径安装为命名卷?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆