Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

247
Views
Using multi-line value in .env file in docker-compose

I have an installer which pumps out some values to a .env file to be used by docker-compose. All of this has worked so far with the exception of an SSH key which cannot be seemingly used.

I have so far tried with both the correctly formatted private key and also replacing new lines with \n. However, this breaks the work flow further down the line and does not appear using printenv within the container as a multi line variable which is required.

docker-compose.yml

myservice:
  build: .
  environment:
    - SSH_KEY
    - SINGLE_LINE_VALUE

.env (ignore the obviously broke sshkey)

SINGLE_LINE_VALUE=I Load just fine
SSH_KEY="---------------
ABCDEFGH
--------------------
"

by the time the container is running, the environment value for SSH_KEY is simply "---------------

Any ideas very much appreciated.

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

If you don't mind not using the .env file (maybe that environment variable is used only in a single container). You can define environment variables directly inside docker-compose.yml and there, you can make full use of YAML formatting options. I.e.:

myservice:
  build: .
  environment:
    SSH_KEY: >
      --------- WHATEVER ----------
      randomkeybase64thingforyourse
      rvice
      ------- END WHATEVER --------

And as a side note, you don't have to copy .env values inside the environment section. .env variables are used inside the docker-compose.yml file, but not in the container's environment. However, you can do:

myservice:
  build: .
  env_file:
    # Files in this array have the same format as `.env` files, but they are
    # passed to container's environment instead being used inside this
    # `docker-compose.yml` file
    - variables.env
  environment:
    SSH_KEY: >
      --------- WHATEVER ----------
      randomkeybase64thingforyourse
      rvice
      ------- END WHATEVER --------
over 4 years ago · Santiago Trujillo Report

0

Maybe not the nicest solution but at least working.

.env

KEY="--------------- \n ABCDEFGH \n --------------------"

docker-compose.yml

version: '3'

services:
  test:
    image: ubuntu
    environment:
      - SSH_KEY=$KEY
    command: bash -c "echo \"$${SSH_KEY}\" | perl -pe 's/\\\n/\n/g' "

$$ means escape for dollar sign so docker-compose won't evaluate that variable in .yml file but in runtime

perl replace is required as docker-compose will automatically add escaping slashes to new line characters.

over 4 years ago · Santiago Trujillo Report

0

I had this issue when trying to add a certificate to my docker.env file which would not be picked up correctly.

Had it like this in the .env file which didn't work:

CERTIFICATE=MIIC5DCCAk2gAwIBAgIJAKgUiZPOajC0MA0GCSqGSIb3DQEBBAUAMFYxCzAJBgNV
BAYTAkRLMRMwEQYDVQQIEwpTb21lLVN0YXRlMQ8wDQYDVQQHEwZBYXJodXMxITAf
...

This worked (note the \n):

CERTIFICATE=MIIC5DCCAk2gAwIBAgIJAKgUiZPOajC0MA0GCSqGSIb3DQEBBAUAMFYxCzAJBgNV\nBAYTAkRLMRMwEQYDVQQIEwpTb21lLVN0YXRlMQ8wDQYDVQQHEwZBYXJodXMxITAf...
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!