【问题标题】:docker-compose volumes syntax for local driver to mount a file本地驱动程序的 docker-compose 卷语法以挂载文件
【发布时间】:2020-04-07 02:39:48
【问题描述】:

我正在尝试在我的docker-compose.ymlvolumes 部分挂载一个文件 (nginx.conf)。我可以毫无问题地将目录挂载为卷,但是我不清楚文件的语法是什么。

我在volumes 部分定义了以下内容

volumes:
    roundcubeweb:
      driver: local
      driver_opts:
        type: none
        o: bind
        device: /mnt/docker/volumes/roundcube/html
    nginxconf:
      driver: local
      driver_opts:
        type: none    
        o: bind
        device: /mnt/docker/volumes/roundcube/nginx.conf

稍后,我的服务部分下有以下内容

nginx:
  image: nginx:latest
  deploy:
    replicas: 1
  restart: always
  depends_on: 
    - roundcube
  ports:
    - "8082:80"
  volumes:
    - roundcubeweb:/var/www/html
    - nginxconf:/etc/nginx/conf.d/default.conf

当我尝试启动它时,我收到以下错误:

错误:对于 roundcube_nginx_1 无法为服务创建容器 nginx:挂载本地卷失败:挂载/mnt/docker /volumes/roundcube/nginx.conf:/var/lib/docker/volumes/roundcube_nginxconf/_data, 标志:0x1000:不是目录

错误:对于 nginx 无法为服务 nginx 创建容器:未能 挂载本地卷:挂载 /mnt/docker/volumes/rou ndcube/nginx.conf:/var/lib/docker/volumes/roundcube_nginxconf/_data, 标志:0x1000:不是目录错误:遇到错误同时 提出项目。

我发现如果我在 nginx 服务部分的卷声明中内联文件位置,那么它工作得很好。例如:

   volumes:  
    - roundcubeweb:/var/www/html  
    - /mnt/docker/volumes/roundcube/nginx.conf:/etc/nginx/conf.d/default.conf  

不能在volumes 部分挂载文件吗?本地驱动的参数有参考吗?

【问题讨论】:

  • 我在我的 Ubuntu 机器上遇到了完全相同的问题(尽管我映射的是目录,而不是文件)。我试图将我的 Docker-compose“环境”(即容器集群)从 Windows 机器移植到 Ubuntu。在 Windows 上,如果我使用顶级 volumes: 表示法,我只能让环境工作(使用 docker-compose up)。在 Ubuntu 上,只有当我按照您在问题中解释的方式“内联”卷容器时,它才会起作用。我的观点是,这似乎不是文件与目录的问题。
  • 经过更多调查后,我注意到关闭 WSL2(在 Windows 主机上)会改变卷绑定方式的行为。在 Windows 中关闭 WSL2 会使行为类似于 Ubuntu -> 使用 volumes: 格式时无法挂载目录,但如果使用内联格式则可以。 (顺便说一句,WSL2 在访问主机文件系统时*非常*慢,应该不惜一切代价避免!)

标签: docker docker-compose


【解决方案1】:

我认为独立的 Docker 卷不可能指向特定文件而不是目录。它可能与如何管理和安装卷的 Docker 设计有关(试图找到文档或相关代码但找不到任何内容)

local 驱动程序的参数似乎与 Linux mount 命令的参数相似。 Docker volume doc 似乎暗示了这一点,虽然不是很清楚:

本地驱动程序接受挂载选项作为 o 参数中的逗号分隔列表


我发现如果我在 nginx 服务部分的卷声明中内联文件位置,那么它就可以正常工作。例如:

volumes:  
 - roundcubeweb:/var/www/html  
 - /mnt/docker/volumes/roundcube/nginx.conf:/etc/nginx/conf.d/default.conf 

在您的示例中,您正在执行Bind Mound 而不是使用Volumes,Docker 对它的处理方式不同,更适合您在容器中安装本地 文件 的用例。我认为在您的情况下使用 Bind Mount 是合适的解决方案。

【讨论】:

    猜你喜欢
    • 2017-11-05
    • 2017-06-30
    • 2017-06-06
    • 2020-07-05
    • 1970-01-01
    • 1970-01-01
    • 2019-01-03
    • 1970-01-01
    • 2017-04-09
    相关资源
    最近更新 更多