简体   繁体   English

docker-compose v3:使用顶级卷语法在多个容器之间共享装入装入的卷

[英]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.

相关问题 docker-compose v3在多个容器之间共享相同的卷安装位置 - docker-compose v3 share the same volume mount locations between multiple containers Docker撰写v3:卷类型mount和bind之间的区别 - Docker compose v3: The difference between volume type mount and bind 我可以在docker镜像中控制已装入装订的卷的所有者吗? - Can I control the owner of a bind-mounted volume in a docker image? docker-compose在多个服务之间共享本地绑定量 - docker-compose share local bind volume between multiple services docker-compose nginx卷未挂载 - docker-compose nginx volumes not mounted 无法在 docker-compose 文件中的容器之间共享卷 - Cannot share volumes between containers in a docker-compose file docker-compose 安装的卷为空,但在 Docker 映像构建期间创建的其他卷已填充 - docker-compose mounted volume is empty, but other volumes created during Docker image build are populated Docker组成容器之间的共享卷,但在主机中具有路径 - Docker-compose shared volume between containers but that has a path in host 为 Docker Compose 条件化绑定安装卷 - Conditionalizing bind mounted volumes for Docker Compose Ddev更新->将〜/ .ddev中的绑定安装数据库迁移到docker-volume安装数据库 - Ddev update -> Migrating bind-mounted database in ~/.ddev to docker-volume mounted database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM