[英]docker-compose v3: sharing bind-mounted volume between multiple containers with top-level volumes syntax
With v2 of docker-compose synthax, we were able to do something like this: 使用docker-compose synthax的v2,我们能够做到这样的事情:
version: '2'
services:
app:
image: tianon/true
volumes:
- ../app:/var/www/app
nginx:
image: nginx
volumes_from:
- app
php:
image: php
volumes_from:
- app
In v3.2 volumes_from
is now invalid option
. 在v3.2中,
volumes_from
现在是invalid option
。 The documentation is all for using new top-level volumes synthax, which is all the ways better
. 该文档仅用于使用新的顶级卷synthax,这是
all the ways better
。 I've read some comments on github, and the only solution that people propose is 我已经阅读了一些关于github的评论,也是人们提出的唯一解决方案
version: '3.2'
services:
nginx:
image: nginx
volumes:
- app:/var/www/app
php:
image: php
volumes:
- app:/var/www/app
volumes:
app:
driver_opts:
type: none
device: ../app
o: bind
Which looks worse obviously, and it even doesn't work for me. 这显然看起来更糟,它甚至对我不起作用。 It gives me an error:
no such file or directory
. 它给了我一个错误:
no such file or directory
。 So what else should I try? 那么我还应该尝试什么呢? It seems like I can still use
links
instead of top-level volumes, but it's considered as legacy option in documentation. 看起来我仍然可以使用
links
而不是顶级卷,但它被认为是文档中的遗留选项。 So how to do it right with new syntax? 那么如何使用新语法做到这一点?
EDIT: Question has been identified as a possible duplicate, but I don't agree. 编辑:问题已被确定为可能重复,但我不同意。 See my comment bellow for explanation.
请参阅我的评论以获得解释。
As the topic starter already mentions, volumes_from
has been removed from the new docker-compose
syntax, according to the documentation in favour of named volumes defined in the top level key volumes
. 正如主题启动者已经提到的那样,
volumes_from
已从新的volumes_from
docker-compose
语法中删除,根据文档支持在顶级密钥volumes
定义的命名volumes
。 The documentation also states the difference between volumes and bind mounts , one of which is who manages the contents: 该文档还说明了卷和绑定装载之间的区别,其中一个是管理内容的人:
By contrast, when you use a volume, a new directory is created within Docker's storage directory on the host machine, and Docker manages that directory's contents.
相反,当您使用卷时,会在主机上的Docker存储目录中创建一个新目录,Docker会管理该目录的内容。
If this is the case, then it does not make sense to bind mount a host folder into a volume and let it be controlled by the host's file system and by Docker simultaneously. 如果是这种情况,那么将主机文件夹挂载到卷中并让它由主机的文件系统和Docker同时控制是没有意义的。
If you still want to bind mount the same folder into two or more containers you could try something like: 如果您仍想将相同的文件夹绑定到两个或更多容器中,您可以尝试以下方法:
version: '3.2'
services:
nginx:
image: nginx
volumes:
- type: bind
source: ../app
target: /var/www/app
php:
image: php
volumes:
- type: bind
source: ../app
target: /var/www/app
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.