docker - docker-compose无法从SOME.env文件检索变量

标签 docker environment-variables docker-compose

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

  • docker-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-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/

    相关文章:

    php - 使用 laradock 迁移 Laravel 数据库失败

    docker - 如何在IBM Bluemix中的现有容器上运行命令?

    windows - 通过 Powershell 添加 JAVA_HOME 到系统变量 Path

    wordpress - Docker本地环境自定义本地域(hosts文件?)

    ubuntu - 启用了始终重启选项的 Docker 卷

    docker-compose 使用循环创建服务

    Docker-Compose 无法复制 haproxy.cfg

    c# - 找不到与命令 "dotnet-/app/Build\ClearPluginAssemblies.dll"Docker 匹配的可执行文件

    c - 简单linux shell中的环境变量

    docker - 如何在入口点脚本中通过 -e 键注入(inject)环境变量?