2017-03-16 78 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