我们需要使用单个撰写文件在单个主机中创建多个我们应用的隔离环境。
我意识到,specifying the project name使用-p
选项,我们可以在一个主机中使用docker compose创建多个隔离的env。
但是,可以覆盖不同环境的撰写文件中的ports:
和volumes:
,而不需要2个独立的docker撰写文件?
例如,我想覆盖以下属性,最好是通过命令行参数。
对于 dev 环境
ports:
8081:8080
volumes:
/etc/myapp/dev/properties/:/etc/myapp/properties
QA 环境
ports:
8082:8080
volumes:
/etc/myapp/qa/properties/:/etc/myapp/properties
答案 0 :(得分:1)
你可以使用template.yml并传递你想要生成的变量docker-compose.yml
首先,使用以下内容创建template.yml
:
version: "2"
...
ports:
"$HOST_PORT":8080
volumes:
"$HOST_VOLUME":/etc/myapp/properties
现在,您可以使用环境所需的变量创建脚本。对于开发环境,它看起来像这样:
#!/bin/bash
# Variables to use in template.yml
export HOST_PORT="8081"
export HOST_VOLUME="/etc/myapp/dev/properties/"
# build docker-compose.yml from the template
source env.sh; rm -rf docker-compose.yml; envsubst < "template.yml" > "docker-compose.yml";
这将生成具有具体值的docker-compose.yml
。
答案 1 :(得分:1)
也需要这个,偶然发现了这个问题。内置了对隔离环境的支持:
Multiple isolated environments on a single host
Compose uses a project name to isolate environments from each other. You can make use of this project name in several different contexts:
* on a dev host, to create multiple copies of a single environment, such as when you want to run a stable copy for each feature branch of a project
* on a CI server, to keep builds from interfering with each other, you can set the project name to a unique build number
* on a shared host or dev host, to prevent different projects, which may use the same service names, from interfering with each other
The default project name is the basename of the project directory. You can set a custom project name by using the -p command line option or the COMPOSE_PROJECT_NAME environment variable.
The default project directory is the base directory of the Compose file. A custom value for it can be defined with the --project-directory command line option.
https://docs.docker.com/compose/#multiple-isolated-environments-on-a-single-host