Sub topic 8
Which Value Wins: Precedence Rules
This chapter assumes you know the difference between interpolation and container variables from the earlier chapters. You also need to be comfortable with environment, env_file, and .env. The same variable name can be set in several places. Compose resolves each case with fixed rules, and the rules differ for interpolation and for container variables.
Precedence for interpolation
Interpolation fills ${VAR} expressions in the Compose file. Compose looks for a value in this order, and the first source that has one wins:
- The shell environment where you run
docker compose. - The
.envfile in the project directory, or the file you pass with--env-file. - The default written in the expression, such as
${DB_PORT:-5432}.
If .env contains DB_PORT=5432, this command still resolves the port to 5433:
DB_PORT=5433 docker compose configThe --env-file option replaces the default .env file. It does not add a third source.
Precedence for container variables
Container variables are the ones the process inside the container sees. Here the Compose file attributes compete with each other. From highest to lowest:
| Rank | Source |
|---|---|
| 1 | `docker compose run -e NAME=value` |
| 2 | An `environment` or `env_file` entry whose value is interpolated, such as `NAME: ${OTHER}` |
| 3 | The `environment` attribute with a literal value |
| 4 | The `env_file` attribute |
| 5 | `ENV` in the image's Dockerfile |
Rank 2 is only a reminder that interpolation happens first. A value like ${OTHER} is resolved with the interpolation rules above, and the result then takes part in the ranking.
The .env file does not appear in this table. It does not reach a container unless you reference it, either with ${VAR} in environment or by listing it under env_file.
A worked example
Suppose common.env contains:
LOG_LEVEL=info
REGION=eu-west-1And compose.yaml contains:
services:
app:
image: alpine:3.20
env_file:
- common.env
environment:
LOG_LEVEL: debug
command: ["sh", "-c", "echo level=$$LOG_LEVEL region=$$REGION"]Running docker compose run --rm app prints level=debug region=eu-west-1. The environment entry beats env_file for LOG_LEVEL. REGION is set only in the file, so it keeps that value.
Now run docker compose run --rm -e LOG_LEVEL=trace app. The output becomes level=trace, because -e has the highest rank.
Common mistakes
- You change
.envbut nothing happens. A variable of the same name is already exported in your shell, and the shell wins. - You put a value in
.envand expect the container to see it. It only reaches the container if the Compose file refers to it. - You set a variable in
env_fileand a different value inenvironment, and theenv_filevalue never appears. This is expected, becauseenvironmenthas higher rank.
Exercise
.env contains DB_PORT=5432. Your shell has DB_PORT=6000 exported. The Compose file has ports: ["${DB_PORT:-5432}:5432"]. Which host port is published, and why?
Show answer
Host port 6000. For interpolation, the shell environment ranks above the .env file, and both rank above the default after :-.