簡體   English   中英

docker-compose,無法在docker容器中打印環境變量

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

我想打印 docker-compose 文件中定義的環境變量。

這是我docker-compose-1.yml

version: '3.6'

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

並且,嘗試在 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

如您所見,收到WARNING: The DEBUG variable is not set. Defaulting to a blank string. 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

我使用雙$符號更改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

如您所見,環境變量打印為$DEBUG ,而不是預期值123456

Compose 將從您運行 compose 的環境中擴展變量。 為擴大變量的容器內,躲避$使用$$

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

有關更多詳細信息,請參閱: https : //docs.docker.com/compose/compose-file/#variable-substitution

編輯:我還在容器內添加了一個 shell 來擴展變量。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM