Docker Compose variable interpolation - defaults, required values and .env files
Answer: Compose files use a Bash-like syntax ${VARIABLE}, with values set in your shell or an .env file in the project directory. Verify with docker compose config, which prints your resolved application config.
Example
$ cat .env
TAG=v1.5
$ cat compose.yaml
services:
web:
image: "webapp:${TAG}"
$ docker compose config
services:
web:
image: 'webapp:v1.5'
Syntax
| Expression | Value | |---|---| | ${VAR} | value of VAR | | ${VAR:-default} | value of VAR if set and non-empty, otherwise default | | ${VAR:?error} | exits with error when VAR is unset or empty | | ${VAR:+replacement} | replacement if VAR is set and non-empty, otherwise empty |
- Other shell-style features, such as
${VARIABLE/foo/bar}, are not supported. - A
$$gives a literal dollar sign and prevents Compose from interpolating a value. - If a variable can't be resolved and has no default, Compose displays a warning and substitutes an empty string.
Where variables come from
When the same variable is declared by multiple sources, precedence applies: your shell environment first, then a file set by --env-file, then an .env file in the project directory. Check the values with docker compose config --environment.
Gotchas
- Interpolation applies only to YAML values, not to keys. For labels or environment keys, an alternate equal sign syntax such as
- "$VAR=BAR"must be used. - Substitution from
.envfiles is a Docker Compose CLI feature, not supported by Swarm withdocker stack deploy.
See also
Claims
- id: C1 claim: Interpolation applies to YAML values but not keys. source: S2 evidence: "Interpolation applies only to YAML values, not to keys."
- id: C2 claim: Use $$ for a literal dollar sign. source: S2 evidence: "You can use a $$ (double-dollar sign) when your configuration needs a literal dollar sign."
- id: C3 claim: The .env file is the default method for setting variables. source: S1 evidence: "The .env file is the default method for setting variables."
Sources
- [S1] Set, use, and manage variables in a Compose file with interpolation. Docker docs. https://docs.docker.com/compose/how-tos/environment-variables/variable-interpolation/ . License: Apache-2.0. Accessed 2026-11-18.
- [S2] Interpolation. Docker Compose file reference. https://docs.docker.com/reference/compose-file/interpolation/ . License: Apache-2.0. Accessed 2026-11-18.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Docker docs, Set, use, and manage variables in a Compose file with interpolation; Docker Compose file reference, Interpolation. Apache-2.0 (https://raw.githubusercontent.com/docker/docs/main/LICENSE). Copyright Docker, Inc. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Docker docs, Set, use, and manage variables in a Compose file with interpolation; Docker Compose file reference, Interpolation. Apache-2.0 (https://raw.githubusercontent.com/docker/docs/main/LICENSE). Copyright Docker, Inc.
Cite this
iinventedlight. 2026. "Docker Compose variable interpolation - defaults, required values and .env files." iinventedlight Library, iil:note:1haxs2ad, version of 2026-11-18. https://iinventedlight.com/library/notes/docker-compose-interpolation/. License: CC-BY-4.0.
@misc{iil_note_1haxs2ad,
author = {{iinventedlight}},
title = {{Docker Compose variable interpolation - defaults, required values and .env files}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-18},
url = {https://iinventedlight.com/library/notes/docker-compose-interpolation/},
note = {Permanent ID iil:note:1haxs2ad. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
Extra files beside this note
These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.
- docker-compose-interpolation.table.json (table; CC-BY-4.0)
- docker-compose-interpolation.schema.json (schema; CC-BY-4.0)
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/docker-compose-interpolation.md.