Docker 组合:单个主机上的多个隔离环境,具有可覆盖的卷和端口
Docker compose: Multiple isolated environments on a single host with overridable volumes and ports
我们需要使用 单个撰写文件 .
在单个主机中为我们的应用程序创建多个独立环境
我意识到,通过 specifying the project name 使用 -p
选项,我们可以使用 docker 在单个主机中组合创建多个独立的环境。
但是,是否可以针对不同的环境覆盖撰写文件中的 ports:
和 volumes:
,而无需 2 个单独的 docker 撰写文件?
例如,我想覆盖以下属性,最好通过命令行参数。
对于开发环境
ports:
8081:8080
volumes:
/etc/myapp/dev/properties/:/etc/myapp/properties
对于QA环境
ports:
8082:8080
volumes:
/etc/myapp/qa/properties/:/etc/myapp/properties
你可以使用一个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
。
也需要这个并且偶然发现了这个问题。内置了对隔离环境的支持:
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
你可以使用Variable substitution with Declare default environment variables in file.
比如我的compose项目结构是
C:\docker-compose
└───multiple-envs
│ .env
│ .env-dev
│ docker-compose.yml
│
├───dev-files
└───files
docker-compose.yml
,下面的文件内容,您可以使用${ENV_VAR}
语法设置端口和其他值。它会自动替换为 docker compose cli.
您可以指示 docker compose 在 ${ENV_VAR}
未设置或使用语法 ${HOST_MYSQL_PORT:?HOST_MYSQL_PORT is not set}
为空时显示错误消息,在 :?
之后是错误消息。
services:
mysqldb:
image: mysql
restart: always
ports:
- ${HOST_MYSQL_PORT:?HOST_MYSQL_PORT is not set}:3306
volumes:
- type: bind
source: ${VOLUMES_SOURCE:?VOLUMES_SOURCE is not set}
target: /mnt
read_only: true
environment:
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD:?MYSQL_ROOT_PASSWORD is not set}
MYSQL_USER: ${MYSQL_USER:?MYSQL_USER is not set}
MYSQL_PASSWORD: ${MYSQL_PASSWORD:?MYSQL_PASSWORD is not set}
.env
文件,在名为 .env
的文件中设置 ENV_VAR=VAL
,假设用于生产。
HOST_MYSQL_PORT=13306
VOLUMES_SOURCE=./files
MYSQL_ROOT_PASSWORD=p419460507
MYSQL_USER=u130085860
MYSQL_PASSWORD=p689273542
.env-dev
文件,在名为 .env-dev
的文件中设置 ENV_VAR=VAL
,假设用于开发。
HOST_MYSQL_PORT=23306
VOLUMES_SOURCE=./dev-files
MYSQL_ROOT_PASSWORD=dev419460507
MYSQL_USER=dev130085860
MYSQL_PASSWORD=dev689273542
要在不同的配置中组合,调用时指定环境文件 docker 组合 cli,--env-file multiple-envs\.env
用于生产或 --env-file multiple-envs\.env-dev
用于开发。
C:\docker-compose> docker-compose --project-directory multiple-envs --env-file multiple-envs\.env up --detach
[+] Running 2/2
- Network multiple-envs_default Created 0.6s
- Container multiple-envs-mysqldb-1 Started 1.8s
C:\docker-compose> docker-compose --project-directory multiple-envs --env-file multiple-envs\.env-dev up --detach
[+] Running 1/1
- Container multiple-envs-mysqldb-1 Started 11.7s
我们需要使用 单个撰写文件 .
在单个主机中为我们的应用程序创建多个独立环境我意识到,通过 specifying the project name 使用 -p
选项,我们可以使用 docker 在单个主机中组合创建多个独立的环境。
但是,是否可以针对不同的环境覆盖撰写文件中的 ports:
和 volumes:
,而无需 2 个单独的 docker 撰写文件?
例如,我想覆盖以下属性,最好通过命令行参数。
对于开发环境
ports:
8081:8080
volumes:
/etc/myapp/dev/properties/:/etc/myapp/properties
对于QA环境
ports:
8082:8080
volumes:
/etc/myapp/qa/properties/:/etc/myapp/properties
你可以使用一个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
。
也需要这个并且偶然发现了这个问题。内置了对隔离环境的支持:
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
你可以使用Variable substitution with Declare default environment variables in file.
比如我的compose项目结构是
C:\docker-compose
└───multiple-envs
│ .env
│ .env-dev
│ docker-compose.yml
│
├───dev-files
└───files
docker-compose.yml
,下面的文件内容,您可以使用${ENV_VAR}
语法设置端口和其他值。它会自动替换为 docker compose cli.
您可以指示 docker compose 在 ${ENV_VAR}
未设置或使用语法 ${HOST_MYSQL_PORT:?HOST_MYSQL_PORT is not set}
为空时显示错误消息,在 :?
之后是错误消息。
services:
mysqldb:
image: mysql
restart: always
ports:
- ${HOST_MYSQL_PORT:?HOST_MYSQL_PORT is not set}:3306
volumes:
- type: bind
source: ${VOLUMES_SOURCE:?VOLUMES_SOURCE is not set}
target: /mnt
read_only: true
environment:
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD:?MYSQL_ROOT_PASSWORD is not set}
MYSQL_USER: ${MYSQL_USER:?MYSQL_USER is not set}
MYSQL_PASSWORD: ${MYSQL_PASSWORD:?MYSQL_PASSWORD is not set}
.env
文件,在名为 .env
的文件中设置 ENV_VAR=VAL
,假设用于生产。
HOST_MYSQL_PORT=13306
VOLUMES_SOURCE=./files
MYSQL_ROOT_PASSWORD=p419460507
MYSQL_USER=u130085860
MYSQL_PASSWORD=p689273542
.env-dev
文件,在名为 .env-dev
的文件中设置 ENV_VAR=VAL
,假设用于开发。
HOST_MYSQL_PORT=23306
VOLUMES_SOURCE=./dev-files
MYSQL_ROOT_PASSWORD=dev419460507
MYSQL_USER=dev130085860
MYSQL_PASSWORD=dev689273542
要在不同的配置中组合,调用时指定环境文件 docker 组合 cli,--env-file multiple-envs\.env
用于生产或 --env-file multiple-envs\.env-dev
用于开发。
C:\docker-compose> docker-compose --project-directory multiple-envs --env-file multiple-envs\.env up --detach
[+] Running 2/2
- Network multiple-envs_default Created 0.6s
- Container multiple-envs-mysqldb-1 Started 1.8s
C:\docker-compose> docker-compose --project-directory multiple-envs --env-file multiple-envs\.env-dev up --detach
[+] Running 1/1
- Container multiple-envs-mysqldb-1 Started 11.7s