2017-03-16 38 views
0

https://docs.docker.com/compose/compose-file/#envfile中所述,我可以将环境变量放入特定文件中。但我无法做到。在过去几个小时内无法弄清楚,即使我已经将docker(Docker版本1.12.5)和docker-compose(版本1.9.0)都升级到了lateste版本。docker-compose无法从SOME.env文件中检索变量

  • 搬运工-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 
    

输出搬运工-构成了将是这样的:

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文件中设置的变量未成功检索。请让我知道我错了。

+0

“.ENV”的作品,但我需要的功能,可指定该文件名来切换环境。 – Cross

回答

2

Docker Compose默认读取.env文件进行变量替换,所以这就是它工作的原因。 https://docs.docker.com/compose/compose-file/#variable-substitution

可以使用$$(双美元符号)时,您的配置需要 文字美元符号。这也会阻止Compose插入 值,因此$$允许您引用您不希望由Compose处理的环境变量。

所以,你必须改变你的文件,撰写一点点:

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

哇,这造成了差异。非常感谢Lauri!现在我完全理解文档:) – Cross