使用`environment`键时未在容器中设置环境变量

时间:2018-12-31 09:15:50

标签: docker docker-compose

these instructions之后,我尝试使用-e TC=3并在撰写文件中设置环境变量,如下所示:

services:
  balancer:
    environment:
      - TC=3

但是在容器运行时未设置该变量。

有人看到我在做什么错吗?

我正在使用:

  • docker-compose 1.23.1,构建b02f1306
  • Docker 18.06.1-ce,构建e68fc7a

1 个答案:

答案 0 :(得分:-1)

您设置环境的方式是正确的。有了这个撰写文件

version: '2'
services:
  test:
    environment:
      - HELLO=WORLD
    image: alpine
    command: env

我得到了这个输出

$ docker-compose -f test-compose.yml up
Creating network "sandbox_default" with the default driver
Creating sandbox_test_1 ... done
Attaching to sandbox_test_1
test_1  | PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
test_1  | HOSTNAME=e2eb1a0da23e
test_1  | HELLO=WORLD
test_1  | HOME=/root
sandbox_test_1 exited with code 0

如果您希望能够覆盖撰写文件中写入的变量,则需要使用${var_name}语法,例如

environment:
  - HELLO=${hello_value}
相关问题