我想在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
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 -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
两种情况都应该存储相同的环境变量(我将此环境变量传递到需要多行字符串的第三方图像中)
答案 0 :(得分:6)
通过将多行环境变量作为构建arg传递给docker build,我能够实现这一点。
<强> Dockerfile 强>
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'
构建命令
Add Media
答案 1 :(得分:3)
转义换行符可以正常工作。
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