[英]Re-using environment variables in docker-compose.yml
Is it possible to re-use environment variables that are shared among multiple containers?是否可以重用在多个容器之间共享的环境变量?
The idea is to avoid duplication, as illustrated in this example:这个想法是为了避免重复,如下例所示:
version: '2'
services:
db:
image: example/db
ports:
- "8443:8443"
container_name: db
hostname: db
environment:
- USER_NAME = admin
- USER_PASSWORD = admin
svc:
image: example/svc
depends_on:
- db
ports:
- "9443:9443"
container_name: svc
hostname: svc
environment:
- DB_URL = https://db:8443
- DB_USER_NAME = admin
- DB_USER_PASSWORD = admin
The extends
option can be nice but it's not supported in 3.x
compose files. extends
选项可能很好,但它在3.x
撰写文件中不受支持。 Other ways to go are:其他的方法是:
Extension fields (compose file 3.4+) 扩展字段(撰写文件 3.4+)
If you can use 3.4+ compose files, extension fields are probably the best option:如果您可以使用 3.4+ 撰写文件,扩展字段可能是最佳选择:
docker-compose.yml docker-compose.yml
version: '3.4' x-common-variables: &common-variables VARIABLE: some_value ANOTHER_VARIABLE: another_value services: some_service: image: someimage environment: *common-variables another_service: image: anotherimage environment: <<: *common-variables NON_COMMON_VARIABLE: 'non_common_value'
docker-compose.yml docker-compose.yml
version: '3.2' services: some_service: image: someimage env_file: - 'variables.env' another_service: image: anotherimage env_file: - 'variables.env'
variables.env变量.env
VARIABLE=some_value ANOTHER_VARIABLE=another_value
.env file in project root (or variables at actual compose environment)项目根目录中的.env 文件(或实际撰写环境中的变量)
Variables from .env file can be referenced in service configuration:可以在服务配置中引用 .env 文件中的变量:
docker-compose.yml docker-compose.yml
version: '3.2' services: some_service: image: someimage environment: - VARIABLE another_service: image: anotherimage environment: - VARIABLE - ANOTHER_VARIABLE
.env .env
VARIABLE=some_value ANOTHER_VARIABLE=another_value
You can use the extends directive (available in compose 1.x
and 2.x
) to have multiple containers inherit the environment
configuration from an underlying service description.您可以使用extends指令(在 compose 1.x
和2.x
可用)让多个容器从底层服务描述继承environment
配置。 For example, put the following in a file named base.yml
:例如,将以下内容放入名为base.yml
的文件中:
version: '2'
services:
base:
environment:
DB_URL: https://db:8443
DB_USER_NAME: admin
DB_USER_PASSWORD: admin
Then in your docker-compose.yml
:然后在你docker-compose.yml
:
version: '2'
services:
container1:
image: alpine
command: sh -c "env; sleep 900"
extends:
file: base.yml
service: base
container2:
image: alpine
command: sh -c "env; sleep 900"
extends:
file: base.yml
service: base
environment:
ANOTHERVAR: this is a test
Then inside of container1
, you will see:然后在container1
内部,您将看到:
DB_URL=https://db:8443
DB_USER_NAME=admin
DB_USER_PASSWORD=admin
And inside of container2
you will see:在container2
里面你会看到:
DB_URL=https://db:8443
DB_USER_NAME=admin
DB_USER_PASSWORD=admin
ANOTHERVAR=this is a test
You can obviously use extends
for things other than the environment
directive;显然,您可以将extends
用于environment
指令以外的内容; it's a great way to avoid duplication when using docker-compose.这是使用 docker-compose 时避免重复的好方法。
You can reference local environment variables from within a docker-compose file.您可以从 docker-compose 文件中引用本地环境变量。 Assuming what you're wanting to do is make USER_NAME
the same as DB_USER_NAME
:假设您想要做的是使USER_NAME
与DB_USER_NAME
相同:
docker-compose.yml docker-compose.yml
version: '2'
services:
db:
image: example/db
ports:
- "8443:8443"
container_name: db
hostname: db
environment:
- USER_NAME = ${USERNAME}
- USER_PASSWORD = ${PASSWORD}
svc:
image: example/svc
depends_on:
- db
ports:
- "9443:9443"
container_name: svc
hostname: svc
environment:
- DB_URL = https://db:8443
- DB_USER_NAME = ${USERNAME}
- DB_USER_PASSWORD = ${PASSWORD}
Then, run docker-compose like:然后,像这样运行 docker-compose:
$ USERNAME="admin" PASSWORD="admin" docker-compose up
Alternately, for something more permanent, and easier to type on a recurring basis:或者,对于更持久且更易于重复输入的内容:
$ printf '%s\n%s\n' 'export USERNAME="admin"' 'export PASSWORD="admin"' >> ~/.bash_profile
$ source ~/.bash_profile
$ docker-compose up
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.