我发现我有多个环境(例如test和prod)很常见,但我希望启动的Docker容器在两种环境中都是相同的。唯一的区别是我想使用env-file
指定的应用程序配置。由于我之间有多个容器和依赖项,因此我想使用docker-compose。但是afaik我只能在env-file
文件中指定docker-compose.yml
(请参阅docs)。如果是这种情况,那么我需要将原始docker-compose.yml
克隆到两个不同的文件(一个用于测试,一个用于prod),只是为了指向不同的env文件。这意味着我必须维护两个docker-compose.yml
文件而不是一个,如果我做了任何更改,我需要更新这两个文件。
这真的是根据设计吗?当我docker-compose
或--env-file
时,为什么docker-compose up
不允许我指定docker-compose run
?
答案 0 :(得分:16)
请参阅下面的更新#2。现在可以了!
这是Docker Compose非常需要的功能。不幸的是,目前的答案是你不能。我建议订阅这些GitHub问题,以便更好地了解何时以及是否实现此功能:
问题#495实际上是目前在其问题库中评论最多的。你绝对不是唯一想要这样做的人。
更新
最新问题跟踪位于https://github.com/docker/compose/issues/1377。
更新#2:
此功能已合并,自Docker Compose 1.5.0起可用。有关使用信息,请参阅https://github.com/docker/compose/blob/129092b7/docs/yml.md#variable-substitution。
答案 1 :(得分:6)
它不是直接包含在命令行中,但是如果您需要在the #1765 merge之前解决(#1377的修复)使其成为一个版本,您可以使用extends
directive以及env_file
directive。为方便起见,以下简单示例中的文件将在this repository。
base.yml
base:
image: busybox
command: bash -c 'echo "${WHO:-Simon} says, \"${SHOUTOUT:-Silence is golden.}\""'
one.env
WHO=Da Schwartz
SHOUTOUT=Get to...
one_glue.yml
one:
extends:
file: base.yml
service: base
env_file:
- one.env
two.env
WHO=Da Schwartz
SHOUTOUT=...da choppa!
two_glue.yml
two:
extends:
file: base.yml
service: base
env_file:
- two.env
% for i in base one_glue two_glue ; do docker-compose --file "${i}.yml" up ; done
Recreating dockercomposeextendsenv_base_1...
Attaching to dockercomposeextendsenv_base_1
base_1 | Simon says, "Silence is golden."
dockercomposeextendsenv_base_1 exited with code 0
Gracefully stopping... (press Ctrl+C again to force)
Recreating dockercomposeextendsenv_one_1...
Attaching to dockercomposeextendsenv_one_1
one_1 | Da Schwartz says, "Get to..."
dockercomposeextendsenv_one_1 exited with code 0
Gracefully stopping... (press Ctrl+C again to force)
Recreating dockercomposeextendsenv_two_1...
Attaching to dockercomposeextendsenv_two_1
two_1 | Da Schwartz says, "...da choppa!"
dockercomposeextendsenv_two_1 exited with code 0
Gracefully stopping... (press Ctrl+C again to force)
如果您从使用.env
文件中受益,则上述工作正常。如果您不受此限制,可以将环境变量设置保存在特定于环境的" glue" .yml
个文件:
red_glue.yml
red:
extends:
file: base.yml
service: base
environment:
- WHO=Stallion
- SHOUTOUT=I am...
blue_glue.yml
blue:
extends:
file: base.yml
service: base
environment:
- WHO=Stallion
- SHOUTOUT=...the law!
% for i in red_glue blue_glue ; do docker-compose --file "${i}.yml" up ; done
Creating dockercomposeextendsenv_red_1...
Attaching to dockercomposeextendsenv_red_1
red_1 | Stallion says, "I am..."
dockercomposeextendsenv_red_1 exited with code 0
Gracefully stopping... (press Ctrl+C again to force)
Creating dockercomposeextendsenv_blue_1...
Attaching to dockercomposeextendsenv_blue_1
blue_1 | Stallion says, "...the law!"
dockercomposeextendsenv_blue_1 exited with code 0
Gracefully stopping... (press Ctrl+C again to force)
对于它的价值,本答案中描述的方法允许基于每个实例的不同.env
文件,而不是每个调用/环境。 (但我不确定这在实践中有多么有用。)换句话说,你可以这样做:
testing.yml
# Only instance1 and instance2 are needed for testing
instance1:
extends:
file: base.yml
service: base
env_file:
- test.env # environment-specific
- instance1_test.env # instance-specific
instance2:
extends:
file: base.yml
service: base
env_file:
- test.env
- instance2_test.env
production.yml
# All four instances are used for production
instance1:
extends:
file: base.yml
service: base
env_file:
- prod.env # environment-specific
- instance1_prod.env # instance-specific
instance2:
extends:
file: base.yml
service: base
env_file:
- prod.env
- instance2_prod.env
instance3:
extends:
file: base.yml
service: base
env_file:
- prod.env
- instance3_prod.env
instance4:
extends:
file: base.yml
service: base
env_file:
- prod.env
- instance4_prod.env
您可以开始看到extends
非常强大,远远超过#1765 merge所允许的范围。
答案 2 :(得分:0)
很好的清晰示例,但是在我更新base.yml以调用ash shell之前,这对我来说并不适合。
<强> base.yml 强>
base:
image: busybox
command: ash -c 'echo "${WHO:-Simon} says, \"${SHOUTOUT:-Silence is golden.}\""'