10

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.

giraff
  • 4,601
  • 2
  • 23
  • 35
  • The `echo` always does that under Linux/Unix if you don't use double-quotes on the environment variable. That is why you should always, use quotes around them (there are many posts on the U&L site relating to this, and this has nothing to do with using YAML: the value for the mapping entry with key `KEY` certainly has a newline) – Anthon Oct 10 '16 at 19:12

3 Answers3

8

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

echo "$KEY"

prints the string with newlines.

giraff
  • 4,601
  • 2
  • 23
  • 35
2

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

KEY: "line1\nline2"

Hope that helps in your case as well.

notion
  • 666
  • 5
  • 8
0

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,
          }
freedev
  • 25,946
  • 8
  • 108
  • 125