Sub topic 4
Choosing files explicitly with -f and COMPOSE_FILE
This chapter assumes you can read Compose YAML, run docker compose up for a multi-service app, and know what the base compose.yaml and the automatic compose.override.yaml do. Here you will choose which files Compose loads and in what order.
Why explicit selection exists
Running docker compose up in a directory loads compose.yaml and then compose.override.yaml. That default suits development. Production should not mount source code or expose a debugger port, so it needs a different set of files.
Compose offers two ways to name files yourself: the -f flag and the COMPOSE_FILE environment variable.
The -f flag
Pass -f once for each file. Compose reads the files in the order you list them, and later files win over earlier ones.
# Development: compose.yaml + compose.override.yaml are used automatically
docker compose up -d
# Production: -f disables the automatic override file, so list every file you want
docker compose -f compose.yaml -f compose.prod.yaml up -dThe production command lists compose.yaml first because it holds the shared definitions. The compose.prod.yaml file follows and adds restart: unless-stopped and quieter logging.
# compose.prod.yaml
services:
web:
restart: unless-stopped
environment:
LOG_LEVEL: warning
db:
restart: unless-stoppedThe COMPOSE_FILE variable
COMPOSE_FILE selects the same files without repeating -f on every command. Separate the paths with : on Linux and macOS. On Windows the default separator is ;. You can change it with COMPOSE_PATH_SEPARATOR.
# The same selection through an environment variable
COMPOSE_FILE=compose.yaml:compose.prod.yaml docker compose up -dYou can also export the variable in your shell session, or put it in a .env file in the project directory. Then every docker compose command in that directory uses the same files. That keeps up, logs, ps, and down consistent. This matters, because running down with a different file set than up can miss services or networks.
If both are present, -f takes precedence over COMPOSE_FILE.
| Method | Scope | Loads override file automatically? |
|---|---|---|
| No flag, no variable | Current command | Yes |
| `-f file` (repeated) | Current command | No |
| `COMPOSE_FILE` | Every command that sees the variable | No |
What the order means
Order is the only rule you need for now. Compose starts with the first file and applies each later file on top. If compose.yaml sets LOG_LEVEL: info and compose.prod.yaml sets LOG_LEVEL: warning, the final value is warning. Reverse the order and the final value is info.
The next chapter covers exactly how values combine, including lists and mappings. For now, treat the last file as the winner for any single value.
Common mistakes
- Expecting
compose.override.yamlto load after you added-f. It does not. - Listing files in the wrong order, so the base file overrides the environment file.
- Setting
COMPOSE_FILEin one terminal and forgetting it in another, so commands see different configurations. - Using
:as the separator on Windows without changingCOMPOSE_PATH_SEPARATOR.
Exercise
You run docker compose -f compose.prod.yaml up -d and the command fails because services have no image. What went wrong, and how do you fix it?
Show answer
With -f, Compose loads only the listed files. The production file contains only additions such as restart policies, so the base definitions are missing. List the base file first: docker compose -f compose.yaml -f compose.prod.yaml up -d.
Exercise
A teammate sets COMPOSE_FILE=compose.yaml:compose.prod.yaml in .env. They then run docker compose -f compose.yaml -f compose.override.yaml up. Which files does Compose load?
Show answer
Only compose.yaml and compose.override.yaml. The -f flag takes precedence over COMPOSE_FILE.