Saqib Ali
Saqib Ali

Reputation: 12545

How can I escape this JSON string in Docker Compose file environment variable?

Here is my docker-compose yaml file.

version: '2.1'

services:
  myservice:
    environment:
      - MYENVVAR={"1": "Hello"}

This gives me the following parsing error when I run docker-compose

ERROR: yaml.parser.ParserError: while parsing a block mapping
  in "./my_docker_compose_.yml", line 6, column 9
expected <block end>, but found '}'
  in "./my_docker_compose_.yml", line 6, column 111

How can I escape my JSON object properly so it gets sent into the container as the value of environment variable MYENVVAR?

Upvotes: 12

Views: 13678

Answers (1)

Alex Konkin
Alex Konkin

Reputation: 736

You should define this variable as: 'FOOBAR={"foo": "bar"}'

In short:

version: '3.3'
services:
    nginx:
        ports:
            - '80:80'
        volumes:
            - '/var/run/docker.sock:/tmp/docker.sock:ro'
        restart: always
        logging:
            options:
                max-size: 1g
        environment:
            - 'FOOBAR={"foo": "bar"}'
            - a=test
        image: nginx

The similar question was raised on docker bug tracking system:

https://github.com/docker/compose/issues/3878

You can validate or experiment with docker-compose settings online by visiting a web page: https://composerize.com/

Upvotes: 25

Related Questions