问题描述
我想在Dockerfile中设置多行环境变量。
I want to set a multiline environment variable in my Dockerfile.
如果我通过 docker run
传递环境变量,一切正常。
If I pass in the environment variable through docker run
everything works.
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'
输出(仅一行,因为它被解释为多行变量)
Output (its only a single line because its interpreted as multiline variable)
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图片
Build and run the docker image
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
期望
两个方案都应存储相同的环境变量(我将这个环境变量传递给需要多行字符串的第三方图像)
Expected
Both scenarios should store the same environment variable (I'm passing this environment variable into a 3rd party image that requires a multiline string)
推荐答案
我能够通过将多行环境变量作为构建arg传递给docker build来使它正常工作。
I was able to get this working by passing the multiline environment variable as a build arg to docker build.
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设置多行环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!