Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Multiline Environment Variable with Dockerfile

Tags:

linux

bash

docker

I want to set a multiline environment variable in my Dockerfile.

Works through command line

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

Doesnt Work through 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'

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

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)

like image 603
Daniel Rasmuson Avatar asked May 11 '18 20:05

Daniel Rasmuson


2 Answers

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'
like image 67
Daniel Rasmuson Avatar answered Nov 08 '22 00:11

Daniel Rasmuson


Escaping newline characters work fine.

Dockerfile

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

Output

Step 8/48 : ENV BUILD_DEPENDENCIES apt-utils   curl   libc-dev   gcc   gnupg2
 ---> Running in 65b0ad105af4
like image 24
Édouard Lopez Avatar answered Nov 08 '22 02:11

Édouard Lopez