I want to set a multiline environment variable in my Dockerfile.
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
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'
Build and run the docker image
docker build -t multilinetest .
docker run multilinetest env | grep 'CONFIG'
Output
CONFIG=port: 4466\ndatabases:\n  prod:\n    connector: mysql\n    active: true\n    host: host\n    port: 3306\n    user: root\n    password: pass
Both scenarios should store the same environment variable (I'm passing this environment variable into a 3rd party image that requires a multiline string)
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
Build Command
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'
                        Escaping newline characters work fine.
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
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With