Sub topic 14
Variables That Control Compose
This chapter assumes you know how interpolation, the .env file, and shell precedence work from earlier chapters. It covers a third group of variables. These are not substituted into your Compose file and not passed to containers. They configure the docker compose command itself.
What control variables do
Compose reads a set of variables that start with COMPOSE_. You can set them in your shell or in the project's .env file. They act like default values for command-line options, so you don't have to retype flags on every command.
| Variable | Equivalent option | Effect |
|---|---|---|
| `COMPOSE_PROJECT_NAME` | `-p` | Sets the project name |
| `COMPOSE_FILE` | `-f` | Lists the Compose files to merge |
| `COMPOSE_PATH_SEPARATOR` | none | Changes the separator used in `COMPOSE_FILE` |
| `COMPOSE_PROFILES` | `--profile` | Enables profiles, separated by commas |
| `COMPOSE_ENV_FILES` | `--env-file` | Selects the env files used for interpolation |
| `COMPOSE_IGNORE_ORPHANS` | none | Stops Compose from warning about orphan containers |
A working example
Put the control variables in .env next to compose.yaml:
# .env
COMPOSE_PROJECT_NAME=shop
COMPOSE_FILE=compose.yaml:compose.dev.yaml
COMPOSE_PROFILES=debug
POSTGRES_USER=appNow a plain docker compose up uses the project name shop, merges both files, and enables the debug profile. POSTGRES_USER is an ordinary interpolation value. The COMPOSE_* lines are read by Compose and are not copied into your containers.
You can check the result without starting anything:
docker compose config | head -n 1The first line printed is name: shop.
Why the project name matters
Compose prefixes the containers, networks, and volumes it creates with the project name. Two directories that both contain a project called app would collide. Setting a distinct name keeps them apart. It also lets you run several copies of one stack side by side, for example one per branch.
When more than one source gives a name, Compose picks the first match from this order:
If none of the first three is set, Compose uses the name of the directory that holds the Compose file.
Separators in COMPOSE_FILE
COMPOSE_FILE uses : between paths on Linux and macOS, and ; on Windows. If your paths contain those characters, or you share one .env across platforms, set COMPOSE_PATH_SEPARATOR:
COMPOSE_PATH_SEPARATOR=|
COMPOSE_FILE=compose.yaml|compose.dev.yamlLimits to know about
COMPOSE_ENV_FILESchooses which env files Compose reads. For that reason, setting it inside a.envfile has no effect. Set it in the shell.- A control variable in the shell beats the same variable in
.env, the same rule as for interpolation values. - A command-line flag beats its matching variable.
docker compose -p demo upusesdemoeven ifCOMPOSE_PROJECT_NAME=shop. - Control variables affect what Compose does, so a stray one in your shell can change behavior without any visible cause.
Exercise
Your .env contains COMPOSE_PROJECT_NAME=shop, and your shell has COMPOSE_PROJECT_NAME=test exported. You run docker compose -p demo up. Which project name does Compose use, and what would it use if you ran the same command without -p?
Show answer
It uses demo, because the -p flag has the highest priority. Without -p, it uses test, because a shell variable overrides the same variable in .env.