2

How do I define a multiline key in docker-compose.yml?

I tried diff solutions:

   environment:
      - PRIVATE_KEY= |-
        -----BEGIN RSA PRIVATE KEY-----
        line2

   environment:
      - PRIVATE_KEY= |
        -----BEGIN RSA PRIVATE KEY-----
        line2

   environment:
      - PRIVATE_KEY= !
        -----BEGIN RSA PRIVATE KEY-----
        line2

   environment:
      - PRIVATE_KEY="-----BEGIN RSA PRIVATE KEY-----\nline2"

   environment:
      - PRIVATE_KEY="-----BEGIN RSA PRIVATE KEY-----\n\nline2"

Resources: https://yaml-multiline.info/ https://gist.github.com/usmansaleem/bb47064f406c105fdfa69716544b7b8e

But none of them formatted the private key in a proper way.

Edit (Addition):

environment:
  - PRIVATE_KEY: |-
    -----BEGIN RSA PRIVATE KEY-----
    line2
Andi Giga
  • 3,744
  • 9
  • 38
  • 68

2 Answers2

2

The only key in your "test" document is the scalar environment, the other scalars are all unquoted multi-line scalar values.

You refer to a document that explains how to do literal scalars, but you never try those, as this

  - PRIVATE_KEY= |-
    -----BEGIN RSA PRIVATE KEY-----
    line2

is the same as doing

  - PRIVATE_KEY= |- -----BEGIN RSA PRIVATE KEY----- line2

i.e. the |- doesn't have any special function except at the beginning of a scalar.
Did you try to do:

  PRIVATE_KEY: |-
    -----BEGIN RSA PRIVATE KEY-----
    line2

that would be a key value pair (note the value indicator (:) instead of the normal character =), with the value being a block style literal scalar

Anthon
  • 69,918
  • 32
  • 186
  • 246
  • Using `:` instead of `=` gives me error when I try to up my containers: ` yaml.scanner.ScannerError: while scanning a simple key` – PlayHardGoPro Jan 14 '22 at 20:25
  • 1
    @PlayHardGoPro you probably have some indentation error, post a new question with your file contents (or at least the relevant parts, including a few lines before the line with the error). Make sure **not** to include the full private key information. Ping me here with a comment when you do. – Anthon Jan 15 '22 at 06:24
  • Hey @Anthon. I just created a question. If you have some spare time to take a look... Thanks :) – PlayHardGoPro Jan 18 '22 at 20:57
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