Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Servers, nginx and Docker

docker compose stops with 'invalid interpolation format' on a value that contains ${...}

ErrorInvalid interpolation format for "environment" option in service

Short answer

Compose reads $VAR and ${VAR} in every value. A braced expression that is not a valid variable name or a supported form, such as ${server.port} or ${VAR/a/b}, stops it with invalid interpolation format. Write the dollar sign twice ($$) so Compose leaves the text alone, then run docker compose config to see the result.

  • Applies to Docker Compose v2, Docker Engine 28
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Let Compose resolve the file on its own, without starting anything. The error names the path of the value it choked on, for example services.app.environment.SERVER_URL.

    docker compose config
  2. 2Match what you see, then jump to its fix

    1. 1invalid interpolation format for services.<name>.<option>.<KEY>, and the value printed under it holds something like ${server.port}, ${env:HOME} or ${VAR/old/new}A ${...} placeholder is meant for the app or the shell, not for ComposeFix
    2. 2No error. The value inside the container is shorter than it should be, and docker compose config prints a warning that a variable is not setA password, hash or command with $name inside is silently changedFix
    3. 3The compose file is fine, but the value read from .env arrives cut shortThe same secret comes from a .env fileFix

What does the error look like?

  • Invalid interpolation format for "environment" option in service
  • invalid interpolation format for services.app.environment.SERVER_URL.
  • You may need to escape any $ with another $.

Cause 1 of 3

A ${...} placeholder is meant for the app or the shell, not for Compose

1Check

List every braced dollar expression in the file. Compose accepts only ${VAR}, the default forms ${VAR:-x} and ${VAR-x}, the required forms ${VAR:?msg} and ${VAR?msg}, and the replacement forms ${VAR:+x} and ${VAR+x}. The name must start with a letter or an underscore and contain only letters, digits and underscores, so a dot or a slash makes the expression invalid. Extended shell features such as search and replace are not supported.

grep -n '\${' docker-compose.yml compose.yaml 2>/dev/null

2Fix

Double the dollar sign in front of every placeholder that is not a Compose variable. Compose turns $$ into one $ and does not expand what follows it.

services:
  app:
    image: my-app:latest
    environment:
      # the app expands its own placeholder at run time
      SERVER_URL: "http://localhost:$${server.port}/api"
    command: sh -c 'echo "$$HOME"'

3Verify

Compose resolves the file, and the printed value carries a single dollar sign.

docker compose config | grep -E 'SERVER_URL|command'

Confirmed by docs.docker.com

Did this fix it?

Cause 2 of 3

A password, hash or command with $name inside is silently changed

1Check

A dollar sign followed by letters is a variable reference. If no such variable exists, Compose warns and substitutes an empty string, so pa$word loses everything from the dollar sign to the end of the name. Generated passwords and password hashes can be hit this way, because they often contain a dollar sign followed by letters.

docker compose config 2>&1 | grep -i -E 'not set|DB_PASSWORD'

2Fix

Escape the dollar sign in the compose file the same way, with two of them.

services:
  db:
    image: postgres:17
    environment:
      # wanted value: pa$word
      POSTGRES_PASSWORD: "pa$$word"

3Verify

The resolved value matches the secret and the warning is gone.

docker compose config | grep POSTGRES_PASSWORD

Confirmed by docs.docker.com

Did this fix it?

Cause 3 of 3

The same secret comes from a .env file

1Check

Unquoted and double-quoted values in .env are interpolated as well. Only single-quoted values are used literally, and the .env next to the compose file feeds Compose's own substitution.

grep -n '\$' .env

2Fix

Put the value in single quotes in the .env file.

# .env
DB_PASSWORD='abc$def'

3Verify

Compose prints the full value.

docker compose config --environment | grep DB_PASSWORD

Confirmed by docs.docker.com

Did this fix it?

How do I stop it coming back?

  • Run docker compose config in CI before docker compose up, so a bad expression fails the pipeline and not the deploy.
  • Keep generated secrets in single quotes in .env, or hand them over as Docker secrets instead of pasting them into the compose file.
  • When a command needs the container's own variables, write them with $$ from the start.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Servers

Compose does not see the $PWD variable in my volume path

Compose warns that a variable is not set and substitutes an empty string

Compose fills ${PWD} from the environment of the shell that runs it, not from the shell's own idea of the current folder.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.