当尝试使用 docker-compose 时,它似乎没有从主机系统读取导出的环境变量。
在撰写本文时,可以找到完整的问题代码 here .
$ docker 信息
Client: Docker Engine - Community
Version: 19.03.6
API version: 1.40
Go version: go1.12.16
Git commit: 369ce74a3c
Built: Thu Feb 13 01:27:58 2020
OS/Arch: linux/amd64
Experimental: false
Server: Docker Engine - Community
Engine:
Version: 19.03.8
API version: 1.40 (minimum version 1.12)
Go version: go1.12.17
Git commit: afacb8b
Built: Wed Mar 11 01:29:16 2020
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: v1.2.13
GitCommit: 7ad184331fa3e55e52b890ea95e65ba581ae3429
runc:
Version: 1.0.0-rc10
GitCommit: dc9208a3303feef5b3839f4323d9beb36df0a9dd
docker-init:
Version: 0.18.0
GitCommit: fec3683
$ docker-compose 信息
docker-compose version 1.21.0, build unknown
docker-py version: 3.4.1
CPython version: 3.7.3
OpenSSL version: OpenSSL 1.1.1d 10 Sep 2019
具体来说,我对文件 docker-compose.yml
及其部分 environment
感兴趣
version: "3"
...
environment:
- FERRO_MONGO_HOST=ferro-mongo
- FERRO_MONGO_USER=ferrothorn
- FERRO_MONGO_PASS=ferrothorn-local
- FERRO_MONGO_BASE=ferrothorn
- FERRO_LOG_LEVEL=1
- FERRO_SECRET
调用 docker-compose up
时,环境变量的设置方式与我希望它们查看 docker-compose 文件的方式相同。但是,如果我导出一些变量,则导出到 shell 的变量不会用于 docker-compose 文件中定义的默认值。对于没有默认值的 FERRO_SECRET
和设置了默认值的 FERRO_LOG_LEVEL
等变量就是这种情况。
我正在使用的命令序列看起来像
export FERRO_SECRET=very_secret_pw
export FERRO_LOG_LEVEL=2
docker-compose up --build # tried with and without --build flag
它只是……不起作用?我也尝试过使用我在网上看到的这个技巧
FERRO_SECRET=very_secret_pw FERRO_LOG_LEVEL=2 docker-compose up --build
但没有骰子。
因为这个盒子实际上是一个使用本地主机 docker 服务器在 windows 10 上运行的 wsl 实例,我认为它可能是那里的一些错误,但是在同一台机器上的 windows powershell 中重新创建这些步骤会产生相同的结果,就像在一个普通的 fedora-server box
我是不是环境变量设置错了?我似乎记得之前在某处做过这个并且它工作,这个 docker-compose 版本或我的配置是否有一些错误?
最佳答案
看起来 Docker 正在将等号后的值解释为文字。
查看 https://docs.docker.com/compose/compose-file/#variable-substitution . 它提到使用 env 文件设置默认值或内联设置它们。 它还为变量使用美元符号和大括号。
例如: - MY_VAR=${MY_ENV_VAR:my_var_default_value}
在我的所有情况下,我都会在替换前加上“$”前缀。在某些情况下,我还用大括号括起来。
FERRO_LOG_LEVEL=${FERRO_LOG_LEVEL:1}
FERRO_SECRET=${FERRO_SECRET}
关于linux - docker-compose 不读取环境变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61023525/