docker-compose - how to escape environment variables
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}"
Related videos on Youtube
zurfyx
#SOreadytohelp I am a full-stack web developer who loves front-end (and javascript). Currently working a lot with React (inc. React Native and Electron), AngularJS and Node. Sometimes I work with D3 and Three too. Not so long ago I have built projects with Django, jQuery and PHP. I like Sass over Less. Wondering whether inline styles are really the future. GitHub: zurfyx LinkedIn: Gerard Rovira Medium: Gerard Rovira Mail: gmail.com@zurfyx
Updated on July 09, 2022Comments
-
zurfyx almost 2 years
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
-
drogon over 5 yearsdocker compose should not allow you to do that without quotes!!
-
ahiijny about 5 years
-
weilbith about 5 years@TomislavMikulin did you figured out how to do with an env file?
-
Akito about 4 yearsSupport for quotes to be expected in 1.26: github.com/docker/compose/issues/2854#issuecomment-628020801
-
Myridium over 2 yearsDoesn't work. Single-quotes do not escape a dollar-sign. e.g.
- 'MYVAR=$GJ'
will try to parse$GJ
as a variable from the docker env file. Seehttps://github.com/docker/compose/issues/5965
. A double-dollar$$
needs to be used to represent one escaped$
.