What ends up in the container
Compose reads .env itself and substitutes the values into the file. Nothing from it gets into the container
until you write env_file: .env. With env_file, everything goes into the container’s environment: password
hashes, neighbouring services’ keys, domains. And every new line in .env changes the container’s environment,
which means it gets recreated (The compose config hash).
A typical case: the Basic Auth hash in .env is needed only by a Traefik label, but through env_file it also
lands inside the app. Remove env_file — the label still gets the value:
services:
app:
# env_file: .env — removed: the whole .env used to go inside
labels:
- "traefik.http.middlewares.app-auth.basicauth.users=${APP_AUTH}" # compose substitutes the value
If it doesn’t work
Added a line to .env — a container that doesn’t need it got recreated
- Cause
- The container has
env_file: .env: every new line changes its environment, and therefore its final config. - Fix
- Remove
env_fileand pass the container only what it needs — throughenvironment:.
How to check
See which variables the container has — names only, no values:
docker compose exec app env | cut -d= -f1 | sort
The list shouldn’t contain variables the app doesn’t need.