Practical developer guides

Docker Compose variables: interpolation versus container ENV

Distinguish YAML parsing, Compose interpolation and environment values passed into a container.

Nav Tools ·

Separate three processing layers

YAML defines the document structure. Compose resolves interpolation expressions, then configures container environment according to its own rules.

A YAML parser accepting a string such as ${PORT} does not prove that Compose can resolve it.

services:
  app:
    image: demo:${TAG:-latest}
    environment:
      PORT: "${PORT:-8080}"

Use default and required forms deliberately

Compose supports default and required-value expressions with distinctions between unset and empty values. Read the exact operator semantics.

The colon-default form uses a default for unset or empty values; a noncolon default treats an empty value differently.

${VAR:-default} → default if unset or empty
${VAR-default}  → default if unset
${VAR:?message} → error if unset or empty

An interpolation source is not automatically container ENV

A project .env can supply values used by Compose interpolation without automatically passing every entry to the container.

Use environment or env_file according to the intended container configuration. A double dollar sign can preserve a dollar for later processing.

command: "echo $HOSTNAME"
Compose leaves $HOSTNAME for the container-side command.

Verify with the actual Compose runtime

Nav Tools converts and inspects configuration text; it does not evaluate the complete local Compose environment or precedence chain.

Run docker compose config in the intended environment to inspect resolved configuration. Keep shared fixtures synthetic and do not publish expanded secrets.

References

← Practical developer guides