简体   繁体   English

docker-compose,无法在docker容器中打印环境变量

[英]docker-compose, can't print environment variable in docker container

I want to print environment variable defined in docker-compose file.我想打印 docker-compose 文件中定义的环境变量。

Here is my docker-compose-1.yml :这是我docker-compose-1.yml

version: '3.6'

services:
  busybox:
    image: busybox
    command: 'echo $DEBUG'
    environment:
      - DEBUG='123456'

And, try to print the DEBUG environment variable in docker container:并且,尝试在 docker 容器中打印DEBUG环境变量:

☁  environment-variables-in-compose [master] ⚡  docker-compose -f docker-compose-1.yml up
WARNING: The DEBUG variable is not set. Defaulting to a blank string.
Creating network "environmentvariablesincompose_default" with the default driver
Creating environmentvariablesincompose_busybox_1 ... done
Attaching to environmentvariablesincompose_busybox_1
busybox_1  |
environmentvariablesincompose_busybox_1 exited with code 0

As you can see, got WARNING: The DEBUG variable is not set. Defaulting to a blank string.如您所见,收到WARNING: The DEBUG variable is not set. Defaulting to a blank string. WARNING: The DEBUG variable is not set. Defaulting to a blank string.

Am I wrong?我错了吗?

update 1:更新1:

docker-compose-1.yml : docker-compose-1.yml

version: '3.6'

services:
  busybox:
    image: busybox
    command: 'echo $$DEBUG'
    environment:
      DEBUG: 123456

I change command using double $ sign, but the result is still not what I want.我使用双$符号更改command ,但结果仍然不是我想要的。

☁  environment-variables-in-compose [master] ⚡  docker-compose -f docker-compose-1.yml up
Creating network "environmentvariablesincompose_default" with the default driver
Creating environmentvariablesincompose_busybox_1 ... done
Attaching to environmentvariablesincompose_busybox_1
busybox_1  | $DEBUG
environmentvariablesincompose_busybox_1 exited with code 0

As you can see, the environment variable is printed as $DEBUG , not expected value 123456如您所见,环境变量打印为$DEBUG ,而不是预期值123456

Compose will expand variables from the environment where you are running compose. Compose 将从您运行 compose 的环境中扩展变量。 To expand the variable inside your container, escape the $ using $$ :为扩大变量的容器内,躲避$使用$$

command: '/bin/sh -c "echo $$DEBUG"'

For more details, see: https://docs.docker.com/compose/compose-file/#variable-substitution有关更多详细信息,请参阅: https : //docs.docker.com/compose/compose-file/#variable-substitution

Edit: I've also added a shell inside the container to expand the variable.编辑:我还在容器内添加了一个 shell 来扩展变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM