How to fix ERROR: Invalid interpolation format in Docker

DockerBEGINNERLOWUpdated Dec 6, 2025ยท 255 views

The 'Invalid interpolation format' error in Docker Compose occurs when the variable interpolation syntax is malformed, typically due to unescaped dollar signs ($) in your values. Fix this by escaping dollar signs with $$ or correcting the variable syntax.

Quick fix

Fixes ERROR: Invalid interpolation format

# WRONG - Compose tries to interpolate $word as a variable
services:
  db:
    environment:
      - DB_PASSWORD=MyP@ss$word

# CORRECT - $$ produces a literal $
services:
  db:
    environment:
      - DB_PASSWORD=MyP@ss$$word
  1. Escape dollar signs with $$
  2. Fix dollar signs in .env files
  3. Identify the problematic value
  4. Use environment directive instead of inline values
docker โ€” error
# WRONG - Compose tries to interpolate $word as a variable
services:
db:
environment:
- DB_PASSWORD=MyP@ss$word
# CORRECT - $$ produces a literal $
services:
db:
environment:
- DB_PASSWORD=MyP@ss$$word
ERROR: Invalid interpolation format

What this error means

The "Invalid interpolation format" error indicates that Docker Compose encountered a dollar sign ($) in your configuration file that it tried to interpret as a variable but couldn't parse correctly. Docker Compose uses Bash-like syntax for variable interpolation, supporting both `$VARIABLE` and `${VARIABLE}` formats. When Compose sees a `$` character, it attempts to process the following characters as a variable name. If the characters don't form a valid variable definition (alphanumeric names matching `[_a-zA-Z][_a-zA-Z0-9]*` or a braced expression starting with `${`), Compose throws this error. Common scenarios that trigger this error include: 1. Passwords or strings containing literal `$` characters (e.g., `MyP@ss$word`) 2. Regular expressions with `$` (e.g., `^/(sys|proc)($|/)`) 3. Shell command syntax with special variables (e.g., `$(...)` or `$@`) 4. A trailing `$` at the end of a string 5. Unsupported shell-style variable expansions like `${VAR/foo/bar}` This error is evaluated at parse time, so even commands like `docker-compose config` or `docker-compose build` will fail before any containers are created.

Typical symptoms

  • Running `docker-compose up` fails immediately with 'Invalid interpolation format' error
  • The error message shows the specific service and option where the problem was found
  • Error occurs in environment variables containing special characters like $
  • `docker-compose config` fails while trying to validate the configuration
  • CI/CD pipeline fails at docker-compose parsing step
  • Commands with regex patterns or shell syntax in docker-compose.yml fail
  • Passwords or API keys with dollar signs cause the error

Common causes

  1. Unescaped dollar sign ($) in environment variable values (most common)
  2. Password or secret containing $ character not properly escaped
  3. Regular expression pattern containing $ for end-of-line matching
  4. Shell command syntax like $(...) or $@ in command or entrypoint
  5. Trailing $ at the end of a string value
  6. Using unsupported variable expansion syntax like ${VAR/foo/bar}
  7. Using mandatory variable syntax ${VAR:?err} in older Compose versions
  8. Dollar sign in .env file values (Compose 2.7+ expands these too)

How to fix "ERROR: Invalid interpolation format"

1Escape dollar signs with $$

The most common fix is to escape any literal dollar signs by doubling them. Docker Compose interprets $$ as a literal $ character:

yaml
# WRONG - Compose tries to interpolate $word as a variable
services:
  db:
    environment:
      - DB_PASSWORD=MyP@ss$word

# CORRECT - $$ produces a literal $
services:
  db:
    environment:
      - DB_PASSWORD=MyP@ss$$word

This applies anywhere in your docker-compose.yml:

yaml
# Command with regex
services:
  node-exporter:
    command:
      # WRONG
      - '--path.sysfs=/host/sys'
      - '--collector.filesystem.ignored-mount-points=^/(sys|proc|dev|host|etc)($$|/)'
      # CORRECT - escape the $ in regex
      - '--collector.filesystem.ignored-mount-points=^/(sys|proc|dev|host|etc)($$|/)'
2Fix dollar signs in .env files

Starting with Docker Compose 2.7, values in .env files are also interpolated. If your .env file contains dollar signs, you have several options:

Option 1: Escape with $$

bash
# .env
DB_PASSWORD=MyP@ss$$word

Option 2: Use single quotes in the .env file

bash
# .env - single quotes prevent interpolation
DB_PASSWORD='MyP@ss$word'

Option 3: Move sensitive values to a separate env_file

yaml
# docker-compose.yml
services:
  db:
    env_file:
      - ./secrets.env  # This file's contents go directly to container
bash
# secrets.env - not processed by Compose interpolation
DB_PASSWORD=MyP@ss$word

Note: The env_file: directive passes values directly to containers without Compose-level interpolation.

3Identify the problematic value

The error message tells you exactly where the problem is. Parse the error output carefully:

bash
ERROR: Invalid interpolation format for "environment" option
in service "web": "DATABASE_URL=postgres://user:pass$word@host/db"

This tells you:
- Option: environment
- Service: web
- Value: The string with the problematic $

Search your docker-compose.yml for that value:

bash
# Find lines containing dollar signs
grep '\$' docker-compose.yml

# Check your .env file too
grep '\$' .env

Look for:
- Passwords with $ (like pa$$word)
- URLs with $ in credentials
- Regex patterns ($ for end-of-line)
- Shell expansions ($(...), $@, $*)

4Use environment directive instead of inline values

For complex values with special characters, you can pass variables from your shell environment instead of hardcoding them:

yaml
# docker-compose.yml
services:
  db:
    environment:
      # Reference shell environment variable (no value assignment)
      - DB_PASSWORD

Then set the variable in your shell:

bash
# Set in shell (no escaping needed)
export DB_PASSWORD='MyP@ss$word'

# Run docker-compose
docker-compose up

This bypasses Compose file interpolation entirely since the value never appears in the YAML file.

5Move complex commands to a script file

If you have complex commands with shell syntax, move them to a separate script file instead of embedding them in docker-compose.yml:

Instead of:

yaml
services:
  app:
    command: >
      sh -c 'for i in $$(seq 1 5); do echo $$i; done'

Create a script:

bash
#!/bin/sh
# scripts/entrypoint.sh
for i in $(seq 1 5); do
  echo $i
done

Reference it in your Dockerfile:

dockerfile
COPY scripts/entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
CMD ["/entrypoint.sh"]

Simplified docker-compose.yml:

yaml
services:
  app:
    build: .
    # No command needed - uses CMD from Dockerfile

This keeps your docker-compose.yml clean and avoids escaping issues.

6Check for unsupported variable syntax

Docker Compose supports some but not all Bash variable expansion features:

Supported syntax:

yaml
# Basic substitution
image: ${REGISTRY}/myapp

# Default value if unset
image: ${REGISTRY:-docker.io}/myapp

# Default value if unset or empty
image: ${REGISTRY-docker.io}/myapp

# Error if unset (Compose v2+)
image: ${REGISTRY:?Registry must be set}

NOT supported:

yaml
# Pattern substitution - will cause error
command: ${PATH/foo/bar}

# Substring extraction - will cause error
value: ${VAR:0:5}

# Array syntax - will cause error
args: ${@}

If you're using unsupported syntax, you'll need to restructure your configuration or use a wrapper script.

7Verify the fix with docker-compose config

After making changes, validate your configuration before running containers:

bash
# Validate and show resolved configuration
docker-compose config

# If successful, you'll see the resolved YAML
# with all variables interpolated

Check that your escaped values appear correctly:

bash
# Show config and filter for your service
docker-compose config | grep -A 10 "db:"

The output should show your literal $ characters where you used $$:

yaml
db:
  environment:
    DB_PASSWORD: MyP@ss$word  # Single $ in output

If docker-compose config succeeds without errors, your interpolation issues are resolved.

Advanced notes

### Understanding Docker Compose Variable Interpolation

Docker Compose processes variables at parse time, before any containers are created. The interpolation follows these rules:

Variable Name Format:
- Must match [_a-zA-Z][_a-zA-Z0-9]*
- For braced expressions ${...}, parsing continues until a matching }
- Any string after $ that doesn't form a valid variable triggers the error

Processing Order:
1. YAML file is loaded
2. Anchors and aliases are resolved
3. Variable interpolation is applied
4. Values are validated against the schema

This means interpolation errors occur before any Docker operations, which is why even docker-compose config fails.

### Differences Between Compose Versions

Compose V1 (Python-based):
- More limited variable syntax support
- ${VAR:?error} mandatory syntax may not work
- Some edge cases behave differently

Compose V2 (Go-based):
- Better error messages with line numbers
- Supports ${VAR:?error} for mandatory variables
- env_file values are also interpolated (since 2.7)

Check your version:

bash
docker compose version  # V2
docker-compose --version  # V1 or V2 with compatibility

### Escaping Rules Summary

| Input | Output | Notes |
|-------|--------|-------|
| $$ | $ | Literal dollar sign |
| $VAR | Value of VAR | Simple substitution |
| ${VAR} | Value of VAR | Braced substitution |
| ${VAR:-default} | VAR or "default" | Default if unset/empty |
| ${VAR-default} | VAR or "default" | Default if unset only |
| ${VAR:?error} | VAR or error | Required variable |

### Common Patterns That Need Escaping

Cron expressions:

yaml
environment:
  # WRONG: $ will be interpolated
  - CRON_SCHEDULE=0 0 * * $
  # CORRECT
  - CRON_SCHEDULE=0 0 * * $$

SQL queries:

yaml
command: psql -c "SELECT * FROM users WHERE name LIKE 'test$$%'"

Regular expressions:

yaml
# Match end of line
command: grep 'pattern$$' /file

# Match literal $
command: grep 'price: \$$[0-9]+' /file

### Debugging Tips

1. Use `docker-compose config` to see resolved values
2. Check .env files - they're interpolated since Compose 2.7
3. Isolate the problem - comment out sections until the error disappears
4. Quote properly in YAML - single quotes prevent YAML processing, not Compose interpolation

yaml
# Both of these are still interpolated by Compose:
value: '$VAR'
value: "$VAR"

# Only $$ prevents interpolation:
value: '$$VAR'  # Literal $VAR

Frequently asked questions

What does "ERROR: Invalid interpolation format" mean?
The "Invalid interpolation format" error indicates that Docker Compose encountered a dollar sign ($) in your configuration file that it tried to interpret as a variable but couldn't parse correctly. Docker Compose uses Bash-like syntax for variable interpolation, supporting both $VARIABLE and ${VARIABLE} formats. When Compose sees a $ character, it attempts to process the following characters as a variable name.
How do I fix "ERROR: Invalid interpolation format"?
The 'Invalid interpolation format' error in Docker Compose occurs when the variable interpolation syntax is malformed, typically due to unescaped dollar signs ($) in your values. Fix this by escaping dollar signs with $$ or correcting the variable syntax.
What causes "ERROR: Invalid interpolation format"?
Unescaped dollar sign ($) in environment variable values (most common) Password or secret containing $ character not properly escaped Regular expression pattern containing $ for end-of-line matching
Is the "ERROR: Invalid interpolation format" error serious?
It is a low-severity error โ€” more of a blocker than a risk. The steps above resolve it.

Official resources & further reading