繁体   English   中英

使用Dockerfile设置多行环境变量

[英]Set Multiline Environment Variable with Dockerfile

我想在Dockerfile中设置一个多行环境变量。

通过命令行工作

如果我通过docker run传入环境变量,一切正常。

CONFIG="port: 4466
databases:
  prod:
    connector: mysql
    active: true
    host: 33.333.333.333
    port: 3306
    user: root
    password: pass"
docker run --env CONFIG="$CONFIG" ubuntu:latest env | grep 'CONFIG'

输出(它只有一行,因为它被解释为多行变量)

CONFIG=port: 4466

不通过dockerfile工作

Dockerfile

FROM ubuntu:latest
ENV CONFIG 'port: 4466\ndatabases:\n  prod:\n    connector: mysql\n    active: true\n    host: host\n    port: 3306\n    user: root\n    password: pass'

构建并运行docker镜像

docker build -t multilinetest .
docker run multilinetest env | grep 'CONFIG'

产量

CONFIG=port: 4466\ndatabases:\n  prod:\n    connector: mysql\n    active: true\n    host: host\n    port: 3306\n    user: root\n    password: pass

预期

这两种情况都应该存储相同的环境变量(我将此环境变量传递给需要多行字符串的第三方图像)

通过将多行环境变量作为构建arg传递给docker构建,我能够实现这一点。

Dockerfile

FROM ubuntu:latest
ARG CONFIG
ENV CONFIG $CONFIG

构建命令

CONFIG="port: 4466
databases:
  prod:
    connector: mysql
    active: true
    host: 33.333.333.333
    port: 3306
    user: root
    password: pass"
docker build --build-arg CONFIG="$CONFIG" ubuntu:latest env | grep 'CONFIG'

转义换行符可以正常工作。

Dockerfile

ENV BUILD_DEPENDENCIES apt-utils \
  curl \
  libc-dev \
  gcc \
  gnupg2

产量

Step 8/48 : ENV BUILD_DEPENDENCIES apt-utils   curl   libc-dev   gcc   gnupg2
 ---> Running in 65b0ad105af4

暂无
暂无

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

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