Andi Giga
Andi Giga

Reputation: 4162

Docker-Compose Multiline Key

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

Upvotes: 2

Views: 4874

Answers (2)

freedev
freedev

Reputation: 30087

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

Anthon
Anthon

Reputation: 76652

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

Upvotes: 2

Related Questions