如何通过docker-compose在容器之间共享卷

时间:2017-11-06 12:10:20

标签: docker docker-compose

我的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?

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

您需要使用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

相关问题