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

sli*_*wp2 3 docker docker-compose

我想打印docker-compose文件中定义的环境变量.

这是我的docker-compose-1.yml:

version: '3.6'

services:
  busybox:
    image: busybox
    command: 'echo $DEBUG'
    environment:
      - DEBUG='123456'
Run Code Online (Sandbox Code Playgroud)

并且,尝试DEBUG在docker容器中打印环境变量:

?  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
Run Code Online (Sandbox Code Playgroud)

如你所见,得到了 WARNING: The DEBUG variable is not set. Defaulting to a blank string.

我错了吗?

更新1:

docker-compose-1.yml:

version: '3.6'

services:
  busybox:
    image: busybox
    command: 'echo $$DEBUG'
    environment:
      DEBUG: 123456
Run Code Online (Sandbox Code Playgroud)

我改变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
Run Code Online (Sandbox Code Playgroud)

如您所见,环境变量打印$DEBUG为非预期值123456

BMi*_*tch 6

Compose将从您正在运行的环境中扩展变量.要展开容器内的变量,请转义$使用$$:

command: '/bin/sh -c "echo $$DEBUG"'
Run Code Online (Sandbox Code Playgroud)

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

编辑:我还在容器中添加了一个shell来扩展变量.