【问题标题】:How to share volumes among containers by docker-compose如何通过 docker-compose 在容器之间共享卷
【发布时间】:2017-11-06 22:30:52
【问题描述】:

我的 docker-compose 定义了两个容器。我希望一个容器与另一个容器共享一个卷。

version: '3'
services:
  web-server:
    env_file: .env
    container_name: web-server
    image: web-server
    build: 
      dockerfile: docker/Dockerfile
    ports: 
      - 3000:3000
      - 3500:3500
    volumes:
      - static-content: /workspace/static
    command: sh /workspace/start.sh

  backend-server:
    volumes:
      - static-content: /workspace/static
  volumes:
    static-content:

上面的 docker composer 文件声明了两个服务,web-server 和 backend-server。我在服务下声明了​​命名卷static-content。运行docker-composer -f docker-composer.yml up时出现以下错误:

services.web-server.volumes contains an invalid type, it should be a string
services.backend-server.volumes contains an invalid type, it should be a string

那么如何共享卷投掷 docker-composer?

【问题讨论】:

    标签: docker docker-compose


    【解决方案1】:

    您的卷字符串中有一个额外的空间,导致 Yaml 将解析从字符串数组更改为名称/值映射数组。删除卷条目中的空格(见下文)以防止出现此错误:

    version: '3'
    services:
      web-server:
        env_file: .env
        container_name: web-server
        image: web-server
        build: 
          dockerfile: docker/Dockerfile
        ports: 
          - 3000:3000
          - 3500:3500
        volumes:
          - static-content:/workspace/static
        command: sh /workspace/start.sh
    
      backend-server:
        volumes:
          - static-content:/workspace/static
      volumes:
        static-content:
    

    更多详情,请参阅compose file section on volumes short syntax

    【讨论】:

      【解决方案2】:

      你需要使用 docker volumes 语法,不带空格

      <local_path>:<service_path>:<optional_rw_attributes>
      

      例如:

      ./:/your_path/
      

      将当前工作目录映射到 /your_path

      还有这个例子:

      ./:/your_path/:ro
      

      将以只读权限将当前工作目录映射到 /your_path

      阅读这些文档了解更多信息: https://docs.docker.com/compose/compose-file/#volume-configuration-reference

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-11-01
        • 1970-01-01
        • 2018-10-28
        • 2021-10-10
        • 2021-02-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多