Sub topic 11
Selecting env files with --env-file and COMPOSE_ENV_FILES
This chapter assumes you can read a compose.yaml, know that Compose reads a .env file for interpolation, and can run docker compose config. It covers how to point Compose at a different file, or at several files, instead of the default .env.
The default and why you might change it
By default, Compose reads .env from the project directory. That works for one environment. Once you have staging, production, or a personal override, you need a way to pick a different file without editing the default.
Remember that these files feed interpolation, the ${VAR} expressions in the Compose file. They are not the same as the env_file attribute, which loads variables into a container.
Using --env-file
The --env-file option replaces the default .env as the source of interpolation values for that command.
docker compose --env-file .env.staging configPlace the option before the subcommand. Use it on every command that should see the same values, such as up, down, and config.
docker compose --env-file .env.staging up -d
docker compose --env-file .env.staging downIf you run up with one file and down with another, Compose can compute different values, such as a different port or project name. Keep the file choice consistent across commands.
You can pass the option more than once. Files are read in order, and a later file overrides an earlier one for the same name.
docker compose --env-file .env --env-file .env.local configThis pattern keeps shared defaults in .env and personal changes in .env.local. When you pass --env-file, the default .env is no longer read automatically, so list it yourself if you still want it.
Using COMPOSE_ENV_FILES
Typing the option on every command is tedious. The COMPOSE_ENV_FILES variable does the same job. It holds a comma-separated list of files.
export COMPOSE_ENV_FILES=.env,.env.local
docker compose configThe --env-file command-line option takes priority over COMPOSE_ENV_FILES when both are set.
Because COMPOSE_ENV_FILES is a variable that configures Compose itself, Compose must read it before it can read any env file. Set it in your shell or in your CI job settings. Putting it inside a .env file has no effect, because Compose would need to read that file first to find out which file to read.
How the choice fits into precedence
Selecting files changes only the file layer. A variable set in the shell still beats the same name from any selected env file. The diagram shows the order of decisions.
You can confirm the effect with config:
DB_PORT=5433 docker compose --env-file .env.staging configHere DB_PORT resolves to 5433 even if .env.staging sets another value.
Common mistakes
- Expecting
--env-fileto load variables into containers. It only feeds interpolation. Useenv_fileorenvironmentfor that. - Forgetting to repeat
--env-fileondown. - Setting
COMPOSE_ENV_FILESinside a.envfile. - Forgetting that naming a file drops the default
.env.
Exercise
You run docker compose --env-file .env.staging up -d, and later run docker compose down with no options. The .env file sets COMPOSE_PROJECT_NAME=shop and .env.staging sets COMPOSE_PROJECT_NAME=shop-stg. What can go wrong, and how do you fix it?
Show answer
The down command reads .env, so it targets the project shop, not shop-stg, and it leaves the staging containers running. Run docker compose --env-file .env.staging down, or export COMPOSE_ENV_FILES=.env.staging in your shell so every command uses the same file.