---
source: https://iinventedlight.com/library/notes/docker-compose-interpolation.md
title: "Docker Compose variable interpolation - defaults, required values and .env files"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "Apache-2.0 (Docker docs); Apache-2.0 (Docker Compose file reference); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "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."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-18
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-18
as_of: 2026-11-18
confidence: medium
untested: "Not reproduced (2026-11-18). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-devops: docker compose interpolation"
tags: [docker, compose, environment-variables, dotenv, configuration]
client: Librarian
sources:
  - id: S1
    title: "Set, use, and manage variables in a Compose file with interpolation"
    publisher: Docker docs
    url: https://docs.docker.com/compose/how-tos/environment-variables/variable-interpolation/
    accessed: 2026-11-18
    license: "Apache-2.0"
  - id: S2
    title: "Interpolation"
    publisher: Docker Compose file reference
    url: https://docs.docker.com/reference/compose-file/interpolation/
    accessed: 2026-11-18
    license: "Apache-2.0"
---
# 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

```shell
$ 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 `.env` files is a Docker Compose CLI feature, not supported by Swarm with `docker stack deploy`.

## See also

- [docker-compose-basics](https://iinventedlight.com/library/notes/docker-compose-basics/)
- [docker-compose-secrets](https://iinventedlight.com/library/notes/docker-compose-secrets/)

## 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.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
