Docker Compose - 在多个容器之间共享命名卷 [英] Docker Compose - Share named volume between multiple containers

查看:37
本文介绍了Docker Compose - 在多个容器之间共享命名卷的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 docker-compose 和 v3.我正在尝试在 docker 中挂载一个卷:

I'm using docker-compose and v3. I'm trying to mount a volume in docker:

./appdata:/appdata

我想将其作为一个卷,然后从多个容器中引用该卷.卷配置参考 仅显示 data-volume: 作为命名卷,没有值,所以看起来不像上面的.

I'd like to have this as a volume and then reference that volume from multiple containers. The volume configuration reference only shows data-volume: as a named volume, with no value, so it doesn't look like the above.

services:

    nginx:
        build: ./nginx/
        ports:
            - 80:80
        links:
            - php
        volumes:
            - app-volume

    php:
        build: ./php/
        expose:
            - 9000
        volumes:
            - app-volume

volumes:
     app-volume: ./appdata:/appdata

这给了我:

错误:在文件./docker-compose.yml"中,卷app-volume"必须是映射而不是字符串.

ERROR: In file './docker-compose.yml', volume 'app-volume' must be a mapping not a string.

显然我知道我需要更改 volumes 键/值对,但我不确定如何更改它以便我可以在服务之间共享一个卷.

Obviously I know I need to change the volumes key/value pair, but I'm not sure how to change this so I can share a volume between services.

我也检查了 volumes_from 但这实际上只是允许从其他容器继承.我见过有人在另一个包含他们想要的映射的容器上使用 volumes_from ,但是设置了 command: true 以便容器永远不会实际运行,对我来说只是看起来像一个黑客.

I've also checked out volumes_from but this effectively just allows inheritance from other containers. I've seen someone use volumes_from on another container that contains the mapping they want, but with command: true set so that the container is never actually run, which to me just seems like a hack.

我该怎么做?

注意,我确实有以下工作:

nginx:
    volumes:
        - ./appdata:/appdata
php:
    volumes:
        - ./appdata:/appdata

但这只是重复,我希望命名卷可以帮助我避免:-)

But that's just duplication and is something I'm hoping a named volume could help me avoid :-)

推荐答案

从 docker-compose 3 版本开始移除了 docker 命名的卷.

The docker named volumes were removed starting from version docker-compose 3.

但是,您可以使用 extension-fields 以避免重复卷源并防止您以后输入错误:

However, you can use extension-fields to avoid duplicating volumes source and prevent yourself from future typos:

version: '3.5'

x-services-volume:
  &services-volume
  type: bind
  source: ./appdata
  target: /appdata

services:

    nginx:
        build: ./nginx/
        ports:
            - 80:80
        links:
            - php
        volumes: *services-volume

    php:
        build: ./php/
        expose:
            - 9000
        # Use same way as for nginx if target override not needed.
        volumes:
            - <<: *services-volume
            target: /opt/target-override

注意:该功能从 3.4 版文件格式开始可用.

NOTE: That feature available starting from version 3.4 file format.

这篇关于Docker Compose - 在多个容器之间共享命名卷的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆