Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 961
Phill Oliver Avatar asked Jun 06 '19 10:06

Phill Oliver


People also ask

Does Docker use .env file?

env in your project, it's only used to put values into the docker-compose. yml file which is in the same folder. Those are used with Docker Compose and Docker Stack.

How do I send .env files to Dockerfile?

The command used to launch Docker containers, docker run , accepts ENV variables as arguments. Simply run it with the -e flag, shorthand for --env , and pass in the key=value pair: sudo docker run -e POSTGRES_USER='postgres' -e POSTGRES_PASSWORD='password' ...

Can you use environment variables in Docker compose?

Docker Compose allows us to pass environment variables in via command line or to define them in our shell. However, it's best to keep these values inside the actual Compose file and out of the command line. “Why?” you may ask.


2 Answers

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 -------- 
like image 195
jaguililla Avatar answered Sep 17 '22 16:09

jaguililla


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.

like image 30
Jakub Bujny Avatar answered Sep 16 '22 16:09

Jakub Bujny