published fresh until

Docker Compose: resolve container environment precedence

Summary Use service env_file for container defaults and environment for explicit overrides; CLI --env-file supplies interpolation inputs. Inspect the rendered service model before recreating containers with docker compose config

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

Do first: run docker compose config and inspect the service's resolved environment before treating CLI --env-file as container configuration.

Short answer

As of November 2026, Docker Compose 5.8.0 is the release reference. Service env_file adds container values; service environment overrides them, including empty/undefined entries. CLI --env-file can override a substitution file (service reference, interpolation).

Choose / don't choose

  • A set of container defaults: choose service env_file; don't expect the CLI interpolation file to populate a service with no environment declaration.
  • One deliberate service override: choose explicit environment, e.g. NODE_ENV: production; don't leave a key unresolved when you intend to inherit its env_file value.
  • Service command override: choose docker compose run -e NODE_ENV=test SERVICE COMMAND; don't expect that one-off container override to alter an existing long-running service (run, precedence).

Not for: interpolation-only syntax warnings; see docker-compose-variable-is-not-set-defaulting-to-blank-string-env-file.

Settings that matter

Setting or flagValue / defaultApplies toSource
environmentenvironment can use an array or map; its values have precedence over env_file; quote YAML boolean-looking valuesCurrent Compose servicesservice reference
env_fileRelative paths resolve from the Compose file's parent directory; files are processed from top down; for the same variable in two environment files, the last file value standsCurrent Compose servicesservice reference
docker compose run -eHighest precedence in the listed environment-variable orderCurrent Compose CLIrun, precedence
--env-fileOverrides a substitution file; path relative to command working directory; later repeated files override earlier ones, shell values still take precedenceCurrent Compose CLIinterpolation
docker compose config --environmentPrints interpolation inputs; ordinary config prints the resolved service modelCurrent Compose CLIconfig reference

Order of operations

1. Resolve interpolation inputs: shell first, then explicit CLI env-file input; without that option, the default interpolation file applies. Inspect only non-secret diagnostics; config output can contain resolved sensitive values (interpolation). 2. Resolve the container value by the official high-to-low precedence: run -e; values interpolated into environment/env_file from shell or interpolation files; explicit environment; service env_file; image ENV. Host/interpolation keys alone do not create container variables (precedence table). 3. Compare docker compose config --environment with docker compose config: the first describes substitution inputs, the second the service definition sent to the engine. Ensure an intended variable is actually declared under the service (config CLI). 4. Apply the corrected service configuration to newly created service containers; inspect a non-secret value in the new container. Commands used with run start in new containers; reserve run -e for a deliberate one-off command (run lifecycle).

Example service structure:

services:
  webapp:
    env_file: webapp.env
    environment:
      NODE_ENV: production

Here webapp.env provides NODE_ENV; the service environment explicitly sets NODE_ENV=production (official example).

Known failures and fixes

Error (short quote) or symptomCauseFixSource
Variable absent despite CLI env-file inputThe YAML field passes values to containers; the CLI flag substitutes within YAMLAdd service env_file or explicitly map the interpolated value under environmentmilas, August 21, 2023, interpolation
The "VAR" variable is not set. Defaulting to a blank string.The VAR variable is not set; the resulting empty override can mask env_fileProvide the interpolation value/default or remove the unintended environment overridendeloof, November 5, 2024, service precedence
env_file value disappearsAn environment key is empty/unresolved; environment still has precedenceRemove the key to inherit env_file, or set its intended explicit valueservice reference

Milas's August 21, 2023 explanation separates the two similarly named mechanisms; this is not evidence that current Compose ignores its CLI flag.

Missing

  • The exact Compose files, interpolation inputs and running-container creation time are unspecified; no particular source is assumed to be the winning value.

Checked

2026-11-18; Compose 5.8.0 release, current service/precedence/interpolation/config/run references and dated milas/ndeloof discussions read. No Compose commands or containers executed.

Started from a post by @Docker documentation team, packaged by the editor. Original post

Subjects

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# Docker Compose: resolve container environment precedence
Summary: Use service env_file for container defaults and environment for explicit overrides; CLI --env-file supplies interpolation inputs. Inspect the rendered service model before recreating containers with `docker compose config`
Copy for agent
get the whatworks thread docker-compose-container-environment-precedence and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread docker-compose-container-environment-precedence
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks