Home > Enterprise >  docker-compose.yml syntax leads to error in runtime
docker-compose.yml syntax leads to error in runtime

Time:09-13

Is there any difference between these two configs:

Config 1:

...
environment:
  - POSTGRES_NAME='postgres'
  - POSTGRES_USER='postgres'
  - POSTGRES_PASSWORD='postgres'
  - POSTGRES_PORT='5432'

Config 2:

...
environment:
  - POSTGRES_NAME=postgres
  - POSTGRES_USER=postgres
  - POSTGRES_PASSWORD=postgres
  - POSTGRES_PORT=5432

Because, when I try to docker-compose up with Config 1 it throws an error (django.db.utils.OperationalError: FATAL: password authentication failed for user "postgres"), and it works fine with Config 2. What is wrong with docker-compose.yml?

CodePudding user response:

In a YAML scalar, the entire string can be quoted, but quotes inside a string are interpreted literally as quotes. (This is a different behavior than, say, the Bourne shell.)

So in this fragment:

environment:
  - POSTGRES_NAME='postgres'

The value of environment: is a YAML list. Each list item contains a YAML string. The string is the literal string POSTGRES_NAME='postgres', including the single quotes. Compose then splits this on the equals sign and sets the variable POSTGRES_NAME to the value 'postgres', including the single quotes.

There's two ways to work around this. One is to not quote anything; even if there are "special characters" after the equals sign, it will still be interpreted as part of the value.

environment:
  - CONTAINING_SPACES=any string
  - CONTAINING_EQUALS=1 1=2
  - CONTAINING_QUOTES="double quotes outside, 'single quotes' inside"

A second way is to use the alternate syntax for ENVIRONMENT that's a YAML mapping instead of a list of strings. You'd then use YAML (not shell) quoting for the value part (and the key part if you'd like).

environment:
  POSTGRES_NAME: 'postgres'     # YAML single quoting
  CONTAINING_SPACES: any string # YAML string rules don't require quotes
  START_WITH_STAR: '*star'      # not a YAML anchor
  'QUOTED_NAME': if you want    # syntactically valid

CodePudding user response:

... environment:

  • POSTGRES_NAME='postgres'
  • POSTGRES_USER='postgres'
  • POSTGRES_PASSWORD='postgres'
  • POSTGRES_PORT='5432'
  • Related