docker-compose无法从SOME.env文件中检索variables

正如https://docs.docker.com/compose/compose-file/#envfile所述 ,我可以把环境variables放到特定的文件中。 但是我做不到。 在过去的几个小时里无法弄清楚,即使我已经将Docker(Docker版本1.12.5)和docker-compose(版本1.9.0)升级到了lateste版本。

  • 泊坞窗,compose.yml:

    version: '2' services: box: image: busybox env_file: tt.env command: echo $RACK_ENV 
  • tt.env

     # Set Rails/Rack environment RACK_ENV=development 

docker的输出组合起来会是这样的:

 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文件中设置的variables不能被成功检索。 请让我知道我错了。

Docker Compose默认读取.env文件进行variablesreplace,所以这就是为什么它工作。 https://docs.docker.com/compose/compose-file/#variable-substitution

当您的configuration需要字面美元符号时,您可以使用$$(双美元符号)。 这也阻止了Compose插入一个值,所以$$允许你引用你不想被Compose处理的环境variables。

所以你必须稍微改变你的撰写文件:

 version: '2' services: box: image: busybox env_file: tt.env command: ["sh", "-c", "echo $$RACK_ENV"]