我正在尝试将docker-come.yml文件与docker-come2.yml文件与bash合并。
docker-compose.yml :
version: "3"
services:
nexus:
image: sonatype/nexus3
volumes:
- "/opt/nexus3/nexus-data:/nexus-data"
ports:
- "8081:8081"
volumes:
nexus-data: {}码头-复合2.yml :
version: "3"
services:
nexus2:
image: sonatype/nexus3
volumes:
- "/opt/nexus3/nexus-data:/nexus-data"
ports:
- "8082:8082"
volumes:
nexus-data: {}我想要的输出:
version: "3"
services:
nexus:
image: sonatype/nexus3
volumes:
- "/opt/nexus3/nexus-data:/nexus-data"
ports:
- "8081:8081"
nexus2:
image: sonatype/nexus3
volumes:
- "/opt/nexus3/nexus-data:/nexus-data"
ports:
- "8082:8082"
volumes:
nexus-data: {}如何使用bash获得此输出?
发布于 2019-11-20 20:46:42
Docker 配置命令完全满足您的需要,它接受多个撰写文件并将它们合并。
只需使用多个-f标志传递它们:
docker-compose -f docker-compose.yml -f docker-compose2.yml config或者使用环境变量:
COMPOSE_FILE=docker-compose.yml:docker-compose2.yml docker-compose config对于每个Docker Compose命令,同样的方法是有效的,因此,如果您的最终目标是,例如,为了设置您的项目,您可以直接运行:
docker-compose -f docker-compose.yml -f docker-compose2.yml up有关如何指定多个组合文件的更多详细信息,请参阅文档。
发布于 2019-11-20 20:31:59
我认为在本地bash中,如果不编写脚本,您很难做到这一点(就像一行代码一样)。我很好奇,所以我做了一个快速搜索,并找到了一个yaml操作工具,它支持合并yaml (停靠-撰写)文件,看起来很适合您的用例。
我使用brew在MacOS上安装,但是也有关于Linux的说明- https://mikefarah.github.io/yq/。
brew install yq显示现有档案:
$ cat file1.yaml
version: "3"
services:
nexus:
image: sonatype/nexus3
volumes:
- "/opt/nexus3/nexus-data:/nexus-data"
ports:
- "8081:8081"
volumes:
nexus-data: {}
$ cat file2.yaml
version: "3"
services:
nexus2:
image: sonatype/nexus3
volumes:
- "/opt/nexus3/nexus-data:/nexus-data"
ports:
- "8082:8082"
volumes:
nexus-data: {}将输出的两个文件合并到stdout:
$ yq m file1.yaml file2.yaml
services:
nexus:
image: sonatype/nexus3
ports:
- 8081:8081
volumes:
- /opt/nexus3/nexus-data:/nexus-data
nexus2:
image: sonatype/nexus3
ports:
- 8082:8082
volumes:
- /opt/nexus3/nexus-data:/nexus-data
version: "3"
volumes:
nexus-data: {}可能有一种本机方式,但我只是将stdout重定向到一个文件:
$ yq m file1.yaml file2.yaml > file3.yaml
$ cat file3.yaml
services:
nexus:
image: sonatype/nexus3
ports:
- 8081:8081
volumes:
- /opt/nexus3/nexus-data:/nexus-data
nexus2:
image: sonatype/nexus3
ports:
- 8082:8082
volumes:
- /opt/nexus3/nexus-data:/nexus-data
version: "3"
volumes:
nexus-data: {}他们的文档中有很多示例供您探索- https://mikefarah.github.io/yq/merge/。
https://stackoverflow.com/questions/58961810
复制相似问题