How to fix ERROR: Invalid interpolation format in Docker
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- Escape dollar signs with $$
- Fix dollar signs in .env files
- Identify the problematic value
- Use environment directive instead of inline values
# WRONG - Compose tries to interpolate $word as a variableservices:db:environment:- DB_PASSWORD=MyP@ss$word# CORRECT - $$ produces a literal $services:db:environment:- DB_PASSWORD=MyP@ss$$wordERROR: 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
- 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
- Shell command syntax like $(...) or $@ in command or entrypoint
- Trailing $ at the end of a string value
- Using unsupported variable expansion syntax like ${VAR/foo/bar}
- Using mandatory variable syntax ${VAR:?err} in older Compose versions
- Dollar sign in .env file values (Compose 2.7+ expands these too)
How to fix "ERROR: Invalid interpolation format"
The most common fix is to escape any literal dollar signs by doubling them. Docker Compose interprets $$ as a literal $ character:
# 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$$wordThis applies anywhere in your docker-compose.yml:
# 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)($$|/)'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 $$
# .env
DB_PASSWORD=MyP@ss$$wordOption 2: Use single quotes in the .env file
# .env - single quotes prevent interpolation
DB_PASSWORD='MyP@ss$word'Option 3: Move sensitive values to a separate env_file
# docker-compose.yml
services:
db:
env_file:
- ./secrets.env # This file's contents go directly to container# secrets.env - not processed by Compose interpolation
DB_PASSWORD=MyP@ss$wordNote: The env_file: directive passes values directly to containers without Compose-level interpolation.
The error message tells you exactly where the problem is. Parse the error output carefully:
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:
# Find lines containing dollar signs
grep '\$' docker-compose.yml
# Check your .env file too
grep '\$' .envLook for:
- Passwords with $ (like pa$$word)
- URLs with $ in credentials
- Regex patterns ($ for end-of-line)
- Shell expansions ($(...), $@, $*)
For complex values with special characters, you can pass variables from your shell environment instead of hardcoding them:
# docker-compose.yml
services:
db:
environment:
# Reference shell environment variable (no value assignment)
- DB_PASSWORDThen set the variable in your shell:
# Set in shell (no escaping needed)
export DB_PASSWORD='MyP@ss$word'
# Run docker-compose
docker-compose upThis bypasses Compose file interpolation entirely since the value never appears in the YAML 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:
services:
app:
command: >
sh -c 'for i in $$(seq 1 5); do echo $$i; done'Create a script:
#!/bin/sh
# scripts/entrypoint.sh
for i in $(seq 1 5); do
echo $i
doneReference it in your Dockerfile:
COPY scripts/entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
CMD ["/entrypoint.sh"]Simplified docker-compose.yml:
services:
app:
build: .
# No command needed - uses CMD from DockerfileThis keeps your docker-compose.yml clean and avoids escaping issues.
Docker Compose supports some but not all Bash variable expansion features:
Supported syntax:
# 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:
# 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.
After making changes, validate your configuration before running containers:
# Validate and show resolved configuration
docker-compose config
# If successful, you'll see the resolved YAML
# with all variables interpolatedCheck that your escaped values appear correctly:
# Show config and filter for your service
docker-compose config | grep -A 10 "db:"The output should show your literal $ characters where you used $$:
db:
environment:
DB_PASSWORD: MyP@ss$word # Single $ in outputIf 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:
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:
environment:
# WRONG: $ will be interpolated
- CRON_SCHEDULE=0 0 * * $
# CORRECT
- CRON_SCHEDULE=0 0 * * $$SQL queries:
command: psql -c "SELECT * FROM users WHERE name LIKE 'test$$%'"Regular expressions:
# 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
# Both of these are still interpolated by Compose:
value: '$VAR'
value: "$VAR"
# Only $$ prevents interpolation:
value: '$$VAR' # Literal $VARFrequently 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
- ๐Docker Official Documentation
- ๐Interpolation - Docker Compose Reference(Official Docs)
- ๐Set environment variables - Docker Docs(Official Docs)
- ๐ฌ
- ๐ฌ
- ๐
- ๐