'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.
Solution 1:[1]
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 --------
Solution 2:[2]
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.
Solution 3:[3]
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...
Solution 4:[4]
According to dotenv's documentation (link), which is used to parse the envfiles within Docker Compose, one should use double quotations with new-lines (\n) to achieve new lines after parsing.
Example:
PRIVATE_KEY="-----BEGIN PRIVATE KEY-----\nMIGHAgEAMBMGByqGSM49AgEGCCqGSU72AwEHBG0wawIBAQQgf+zcD+dC95xJH2YIGtZxg6cBEtZYzeMHZIdAtGarkfahRANCAAReRj+7DNK3rfudJGSo2k9mUBhmz2zqE3d4sfpkIGUizc1s8UXYmnhgezq6fceV9o6HG1I6BdVWi5cfbwTB17Qf\n-----END PRIVATE KEY-----"
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | jaguililla |
| Solution 2 | Jakub Bujny |
| Solution 3 | Philipp Jahoda |
| Solution 4 | henrhoi |
