简体   繁体   English

如何在两个容器之间正确共享相同的主机卷?

[英]How do I properly share the same host volume between the two containers?

Since volumes_from disappear when Docker Compose change it's compose file version I am a bit lost in how to share a volume between different containers. 由于volumes_from在Docker Compose更改时消失了,因此它的撰写文件版本使我在如何在不同容器之间共享卷时有点迷茫。

See the example below where a PHP application is living in a PHP-FPM container and Nginx is living in a second one. 请参见下面的示例,其中PHP应用程序驻留在PHP-FPM容器中,而Nginx驻留在第二个容器中。

version: '3.3'

services:
    php:
        build:
            context: ./docker/php7-fpm
            args:
                TIMEZONE: ${TIMEZONE}
        env_file: .env
        volumes:
          - shared-volume:/var/www
    nginx:
        build: ./docker/nginx
        ports:
            - 81:80
        depends_on:
            - php
        volumes:
          - shared-volume:/var/www
volumes:
  shared-volume:
    driver_opts:
      type: none
      device: ~/sources/websocket
      o: bind

In order to make the application works of course somehow Nginx has to access the PHP files and there is where volumes_from help us a lot. 为了使应用程序正常工作,Nginx必须以某种方式访问​​PHP文件,而volumes_from可以为我们提供很多帮助。 Now that option is gone. 现在,该选项已消失。

When I try the command docker-compose up it ends with the following message: 当我尝试命令docker-compose up它以以下消息结尾:

ERROR: for websocket_php_1 Cannot create container for service php: error while mounting volume with options: type='none' device='~/sources/websocket' o='bind': no such file or directory 错误:对于websocket_php_1无法为服务php创建容器:安装带有选项的卷时出错:type ='none'device ='〜/ sources / websocket'o ='bind':无此类文件或目录

How do I properly share the same host volume between the two containers? 如何在两个容器之间正确共享相同的主机卷?

Why would you not use a bind mount? 为什么不使用绑定安装? This is just source code that each needs to see, correct? 这只是每个人都需要查看的源代码,对吗? I added the :ro (read-only) option which assumes no code generation is happening. 我添加了:ro(只读)选项,该选项假定没有代码生成发生。

services:
    php:
        build:
            context: ./docker/php7-fpm
            args:
                TIMEZONE: ${TIMEZONE}
        env_file: .env
        volumes:
            # User-relative path
            - ~/sources/websocket:/var/www:ro

    nginx:
        build: ./docker/nginx
        ports:
            - 81:80
        depends_on:
            - php
         volumes:
            # User-relative path
            - ~/sources/websocket:/var/www:ro

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM