Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

docker-compose - how to escape environment variables

With docker-compose v2 environment variables can be set by simply:

enviroment:   - MONGO_PATH=mongodb://db-mongo:27017 

The full docker-compose.yml file being:

version: '2' services:   web:     build: .     environment:       - MONGO_PATH=mongodb://db-mongo:27017     ports:       - "3000:3000"     volumes:       - .:/app       - /app/node_modules     depends_on:        - db-mongo       - db-redis   db-mongo:     image: mongo     restart: unless-stopped     command: --smallfiles     ports:       - "27017:27017"     volumes:       - ./data:/data/db   [...] 

However, how can I escape environment variables that are not a plain string?

{"database": {"data": {"host": "mongo"}}} 

I tried:

NODE_CONFIG=\{"database": \{"data"\: \{"host": "mongo"\}, "session": \{"host": "redis" \}\}\} NODE_CONFIG="\{"database": \{"data"\: \{"host": "mongo"\}, "session": \{"host": "redis" \}\}\}" NODE_CONFIG='{"database": {"data": {"host": "mongo"}, "session": {"host": "redis" }}}' 

ERROR: yaml.parser.ParserError: while parsing a block mapping in "./docker-compose.yml", line 6, column 9 expected , but found '}' in "./docker-compose.yml", line 6, column 92

like image 727
zurfyx Avatar asked Feb 01 '17 20:02

zurfyx


People also ask

How do I override Docker compose environment variables?

Overriding a single value in your docker-compose . env file is reasonably simple: just set an environment variable with the same name in your shell before running your docker-compose command.

Does Docker compose inherit environment variables?

Using docker-compose , you can inherit env variables in docker-compose. yml and subsequently any Dockerfile(s) called by docker-compose to build images. This is useful when the Dockerfile RUN command should execute commands specific to the environment.

How do I exit after Docker compose up?

Just Stopping the Container TL;DR: press ctrl+c then ctrl+d - that means, keep the ctrl key pressed, type a c, and let go of ctrl. Then the same with ctrl and d. If there's a non-shell process running, the combination is ctrl+c to interrupt it. Then you can exit the shell, or the container might exit already.


1 Answers

Environment variables (including their name), have to be fully wrapped inside single or double quotes: "" or ''

environment:   - 'NODE_CONFIG={"database": {"data": {"host": "mongo"}, "session": {"host": "redis" }}}' 

And using double quotes:

environment:   - 'PORT=3000'   - "NODE_CONFIG={\"database\": {\"data\": {\"host\": \"mongo\"}, \"session\": {\"host\": \"redis\" }}}" 

It is remarkable to note that using double quotes "", like bash, will allow placing variables inside the environment variable.

"MY_HOME_ENV_VARIABLE=${HOME}" 
like image 71
zurfyx Avatar answered Oct 11 '22 13:10

zurfyx