skip to Main Content

I’m trying to set a public key as environment variable inside my docker-compose.yaml file.

I understand we have 2 different syntax we can use (can’t use both at same time):
One is a YAML mapping, which uses the syntax key: value:

environment:  
  key: value
  key2: value2
  // And this is how we Muliline
  publicKey: |-
    -----BEGIN PUBLIC KEY-----
    UAdhusaudu89Ajdsdoskdosadk*(Sd98798da
    kaosdkOKdoksaoKDOaksdo98d7868sadajidas

And we also have the YAML list syntax, which uses the syntax var = value:

environment:
  key=value

But how may I successfully multiline using YAML list syntax?
I need to get the line breaks (n) too.

When I try to read the environment variable I get a string where instead of having line breaks (/n) I get a single whitespace.

I get:

-----BEGIN PUBLIC KEY----- KOSKODKSOAKD DOKSODKSAOD...

what I actually need:
-----BEGIN PUBLIC KEY-----nKOSKODKSOAKDnDOKSODKSAOD... (note the /n).

Why I need this?
Without it, the public key verification/validation will fail.
Now I have a function that runs through the string and manually adds the line breakers, but it ain’t pretty, if I could make it work from the docker-compose file it would be much better.

2

Answers


  1. Here you get help:

    How do I break a string in YAML over multiple lines?

    Use >- or |- instead if you don’t want a linebreak appended at the end.

    Login or Signup to reply.
  2. This:

    environment:
    key=value

    is a mapping with one key environment, which has as value key=value. That is not further parsed, = not having special meaning within YAML ( of course your application can further interpret such a scalar )

    If you want list syntax in your block-style YAML, you’ll need to use the block sequence entry indicator followed by a space (- ):

    environment:
      - key=value
    

    again key=value is a single scalar for YAML.

    To set the environment variable DEBUG, the docker-compose documentation uses:

    web:
      environment:
        - DEBUG=1
    

    The split into the environment variable name DEBUG and its value (1) is done by docker-compose. If you want an environment variable to have newlines, you’ll need the complete sequence element to be a multiline scalar, which you can do in the following way:

    environment:
        - key=value
        - key2=value2
        - |-
          publicKey=-----BEGIN PUBLIC KEY-----
          UAdhusaudu89Ajdsdoskdosadk*(Sd98798da
          kaosdkOKdoksaoKDOaksdo98d7868sadajidas
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search