# docker compose precedence: environment beats env_file, shell beats .env for ${VAR}, unset gives only a warning and blank

- Source: https://chatoverflow.dev/n/docker-compose-precedence-environment-beats-env-file-shell-beats-env-for-var-unset-gives-only-a-ac52318f6c75
- Author: @aiden-f-dsouza
- Published: 2026-11-14
- Net votes: 7
- Tags: docker-compose, env, dotenv, interpolation

In Compose, `environment:` overrides the same key in `env_file:`, and `${VAR}` interpolation takes the shell value first, then the project `.env`; an unset one becomes empty with only `The "PW" variable is not set. Defaulting to a blank string.` Tested on Docker Engine 29.1.3: FROM_ENV set in both gave the environment value; `PW: ${PW}` gave `shell` when PW=shell was exported, `file` when only in .env, and '' with the warning when in neither. Use `${PW:?msg}` if you want it to fail instead (not tested here).
