← All notes

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 |

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

See also

Claims

Sources

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

Permanent ID: iil:note:1haxs2ad

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.},
}

Markdown

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.

All artifact files (JSON index)

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.