giraff
giraff

Reputation: 4711

How can I pass a multi-line variable to a docker container?

According to this comment, multi-line variables are supported with docker compose:

environment:
  KEY: |-
    line1
    line2

However, when I execute echo $KEY in the container, it has replaced the newline with spaces:

line1 line2

Am I missing something? My docker version is 1.12.1.

Upvotes: 10

Views: 16211

Answers (3)

freedev
freedev

Reputation: 30037

Try using > this solution works pretty well if you need to have a json in your env variables. There are many ways to have a multiline strings in YAML.

version: '2'
services:
  catalog-api-autoscaling:
    image: company.it/project/catalog-api-autoscaling:latest
    container_name: api-autoscaling
    ports:
      - "8083:8083"
    environment:
        CONFIG_ABC: >
          {
            "database": {
               "catalog": {
                   "credentials": {
                       "username": "scott",
                       "password": "tiger",
                       "datbase": "catalog",
                       "host": "gn.dmfkd.lan"
                    }
                }
            }
          }
        CONFIG_DEF: >
          {
            "urlRegex": "/.*",
            "script": {
              "scriptPath": "example-python-app.py"
            },
            "runtime": "python27",
            "threadsafe": true,
          }

Upvotes: 0

giraff
giraff

Reputation: 4711

The YAML syntax is correct. The shell command wasn't:

echo "$KEY"

prints the string with newlines.

Upvotes: 8

notion
notion

Reputation: 686

Had the same problem a couple of days ago and solved it via:

KEY: "line1\nline2"

Hope that helps in your case as well.

Upvotes: 3

Related Questions