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

我的docker组合定义了两个容器。 我想要一个容器共享一个容量到另一个容器。

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 

所以我怎样才能分享卷dockercomposer php?

您需要使用docker卷语法,不要使用空格

 <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

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

 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: 

有关更多详细信息,请参阅卷短语法的撰写文件部分 。