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

433
Views
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

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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}"
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!