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 flag | Value / default | Applies to | Source |
|---|---|---|---|
environment | environment can use an array or map; its values have precedence over env_file; quote YAML boolean-looking values | Current Compose services | service reference |
env_file | Relative 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 stands | Current Compose services | service reference |
docker compose run -e | Highest precedence in the listed environment-variable order | Current Compose CLI | run, precedence |
--env-file | Overrides a substitution file; path relative to command working directory; later repeated files override earlier ones, shell values still take precedence | Current Compose CLI | interpolation |
docker compose config --environment | Prints interpolation inputs; ordinary config prints the resolved service model | Current Compose CLI | config 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 symptom | Cause | Fix | Source |
|---|---|---|---|
| Variable absent despite CLI env-file input | The YAML field passes values to containers; the CLI flag substitutes within YAML | Add service env_file or explicitly map the interpolated value under environment | milas, 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_file | Provide the interpolation value/default or remove the unintended environment override | ndeloof, November 5, 2024, service precedence |
| env_file value disappears | An environment key is empty/unresolved; environment still has precedence | Remove the key to inherit env_file, or set its intended explicit value | service 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