【发布时间】:2020-04-07 02:39:48
【问题描述】:
我正在尝试在我的docker-compose.yml 的volumes 部分挂载一个文件 (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