我有以下破碎的docker-compose文件
version: '3.4'
x-vols1: &vols-1
- /home/:/home/
x-vols2: &vols-2
- /tmp/:/tmp/
services:
app1:
container_name: app1
image: app1
volumes:
<<: *vols-1
app2:
container_name: app2
image: app2
volumes:
<<: *vols-1
<<: *vols-2
失败并出现以下错误
$ docker-compose -f test.yaml config
ERROR: yaml.constructor.ConstructorError: while constructing a mapping
in "./test.yaml", line 14, column 13
expected a mapping for merging, but found scalar
in "./test.yaml", line 4, column 7
问题1:如何在中合并数组docker-compose
?我尝试使用的语法是合并字典的语法
问题2:如果无法合并数组,是否有解决方法?
用例:我有多个服务,其中一些服务映射一些卷,其他服务映射其他卷,其他服务映射所有卷。我不想重复我自己。
谢谢!
可以通过使用多个docker-compose
文件(每个卷一个文件)来实现所需的行为。请注意,锚和别名不是必需的,但要使其与问题保持一致。
base.yaml
version: '3.4'
services:
app1:
container_name: app1
image: app1
app2:
container_name: app2
image: app2
vol1.yaml
version: '3.4'
x-vols1: &vols-1
volumes:
- /home/:/home/
services:
app1:
container_name: app1
image: app1
<<: *vols-1
app2:
container_name: app2
image: app2
<<: *vols-1
vol2.yaml
version: '3.4'
x-vols2: &vols-2
volumes:
- /tmp/:/tmp/
services:
app2:
container_name: app2
image: app2
<<: *vols-2
验证为
$ docker-compose -f base.yaml -f vol1.yaml -f vol2.yaml config
结果
services:
app1:
container_name: app1
image: app1
volumes:
- /home:/home:rw
app2:
container_name: app2
image: app2
volumes:
- /home:/home:rw
- /tmp:/tmp:rw
version: '3.4'
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句