如https://docs.docker.com/compose/compose-file/#envfile中所述,我可以将环境变量放入特定文件中。但是我做不到。在过去的几个小时内无法弄清楚,即使我已经将docker(Docker版本1.12.5)和docker-compose(版本1.9.0)都升级到了最新版本。
version: '2'
services:
box:
image: busybox
env_file: tt.env
command: echo $RACK_ENV
# Set Rails/Rack environment
RACK_ENV=development
docker-compose up 的输出将如下所示:
WARNING: The RACK_ENV variable is not set. Defaulting to a blank string.
Creating network "test_default" with the default driver
Creating test_box_1
Attaching to test_box_1
box_1 |
test_box_1 exited with code 0
这意味着无法成功检索tt.env文件中设置的变量。请让我知道我错了。
最佳答案
Docker Compose默认情况下会读取.env
文件以进行变量替换,因此这就是它起作用的原因。
https://docs.docker.com/compose/compose-file/#variable-substitution
You can use a $$ (double-dollar sign) when your configuration needs a literal dollar sign. This also prevents Compose from interpolating a value, so a $$ allows you to refer to environment variables that you don’t want processed by Compose.
因此,您必须稍微更改撰写文件:
version: '2'
services:
box:
image: busybox
env_file: tt.env
command: ["sh", "-c", "echo $$RACK_ENV"]
关于docker - docker-compose无法从SOME.env文件检索变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42836982/