Sub topic 4
Choosing files explicitly with -f and COMPOSE_FILE
This chapter assumes you can read basic YAML, you have run docker compose up and down for a multi-service app, and you know what a Compose project is. It also assumes you have seen the base compose.yaml and the automatic compose.override.yaml from the previous chapter. Here you will take control of which files Compose loads.
Why the automatic behavior is not enough
With no options, Compose looks in the current directory for compose.yaml and then for compose.override.yaml. If both exist, it merges the override over the base. That default suits a developer laptop.
Production and CI need something else. You do not want source code mounts or a debugger port on a production host. You want compose.yaml plus compose.prod.yaml, and nothing else. Two mechanisms select files explicitly:
- The
-f(or--file) flag on the command line. - The
COMPOSE_FILEenvironment variable.
Both replace the default lookup. After you use either one, Compose loads exactly the files you list.
The -f flag
Pass -f once per file. Compose reads the files in the order you give them, and later files win over earlier ones.
export DB_PASSWORD=change-me
# 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 first command needs no flags because the default lookup finds both files. The second command loads compose.yaml and then compose.prod.yaml. compose.override.yaml is not loaded, even though it sits in the same directory. That is the point: the development mounts and the debugger port stay out of production.
The export DB_PASSWORD=change-me line is there because the base file uses ${DB_PASSWORD:?set DB_PASSWORD}. If the variable is unset, Compose stops with the message set DB_PASSWORD instead of starting a database with an empty password.
Note that -f goes between docker compose and the subcommand. It is an option of docker compose, not of up.
Order matters
Compose merges from left to right. Each new file is applied over the result so far. Compare these two commands:
docker compose -f compose.yaml -f compose.prod.yaml config
docker compose -f compose.prod.yaml -f compose.yaml configThe first gives a working configuration. In the second, compose.prod.yaml is the starting point and compose.yaml is applied over it. For a scalar value set in both files, such as LOG_LEVEL, the base would then win over production. The second command would also lose the build and image information that only the base supplies, depending on what each file defines. Put the most general file first and the most specific file last.
Files are not standalone
compose.prod.yaml from this tutorial defines only restart and LOG_LEVEL. It has no image, no ports, and no volumes. If you run it alone, it fails:
docker compose -f compose.prod.yaml up -dCompose reports that the web service has neither an image nor a build section. An override file is a patch, and a patch needs something to apply to. Always list the base file first.
Combining the default override with -f
If you do want the development override together with another file, name it yourself:
docker compose \
-f compose.yaml \
-f compose.override.yaml \
-f compose.test.yaml \
up -dNothing is added for you once -f appears.
The COMPOSE_FILE variable
Typing the same -f flags every time is tedious and error-prone. COMPOSE_FILE holds the same list in an environment variable:
# The default separator is : on Linux and macOS
COMPOSE_FILE=compose.yaml:compose.prod.yaml docker compose up -dThe variable has the same effect as repeating -f: the files load in the order written, and the automatic override file is skipped.
| Setting | Behavior |
|---|---|
| No `-f`, no `COMPOSE_FILE` | Loads `compose.yaml` and `compose.override.yaml` |
| `COMPOSE_FILE` set | Loads the listed files only |
| `-f` given | Loads the listed files only |
| Both `-f` and `COMPOSE_FILE` | The `-f` flags win and `COMPOSE_FILE` is ignored |
The last row is useful. You can export COMPOSE_FILE in a shell session and still run a one-off command with different files by passing -f.
The path separator
On Linux and macOS the separator is a colon. On Windows it is a semicolon, because colons appear in drive letters such as C:\. You can change the separator with COMPOSE_PATH_SEPARATOR:
COMPOSE_PATH_SEPARATOR=';' \
COMPOSE_FILE='compose.yaml;compose.prod.yaml' \
docker compose config --servicesUse this when a file path itself contains a colon, or when a team mixes operating systems and wants one format in a shared script.
Setting COMPOSE_FILE in a .env file
Compose reads a .env file in the project directory. It treats COMPOSE_FILE there as a Compose setting, so you can pin the files for a directory:
# .env
COMPOSE_FILE=compose.yaml:compose.prod.yaml
DB_PASSWORD=change-meAfter that, a plain docker compose up -d loads the production files. This works well on a server where one checkout always runs one environment. It is risky on a laptop, because someone running docker compose up in that directory will silently get production settings. Keep a committed .env for shared defaults only, and use a git-ignored .env.local or per-environment variables for choices that differ by machine.
Paths and project names
Two behaviors surprise people when files live outside the current directory.
- Relative paths resolve against the first file's directory. If you run
docker compose -f deploy/compose.yaml -f deploy/compose.prod.yaml up, thenbuild: .and./src:/app/srcare resolved relative todeploy/, not to your current directory. Every file in the list is resolved relative to that same base, even if a later file sits in another folder. To choose a different base, pass--project-directory. - The project name defaults to the project directory's name. Compose prefixes container, network, and volume names with it. Changing which file comes first can therefore change the project name and make
docker compose psappear empty. Set the name explicitly with-porCOMPOSE_PROJECT_NAMEwhen it matters.
docker compose \
--project-directory . \
-p shop \
-f deploy/compose.yaml \
-f deploy/compose.prod.yaml \
up -dUse the same files for every command
The file list is not stored anywhere. Each command computes the project from the files you pass at that moment. If you start with two files and later run docker compose down with no flags, Compose reads the default files and builds a different picture of the project. Services that exist only in your production file are not part of that picture, so they may be left running.
docker compose -f compose.yaml -f compose.prod.yaml up -d
docker compose -f compose.yaml -f compose.prod.yaml logs web
docker compose -f compose.yaml -f compose.prod.yaml downSetting COMPOSE_FILE once in the shell or in .env removes this risk, because every command in that environment sees the same list.
Verify before you start anything
Before starting a stack with a new file combination, print what Compose will use:
docker compose -f compose.yaml -f compose.prod.yaml configThe output shows the merged, interpolated configuration, and Compose also prints the files it loaded when COMPOSE_FILE is involved if you add --no-interpolate. A later chapter covers this command in detail. For now, use it as a quick check that your file order produced the values you expect: LOG_LEVEL should read warning, restart should read unless-stopped, and no 5678 port should appear.
Common mistakes
- Expecting the override file to load after
-f. It does not. List it yourself if you want it. - Listing the files in reverse order. The most specific file must come last.
- Using
;on Linux or:on Windows inCOMPOSE_FILE. The variable is then read as one very long file name, and Compose reports that it cannot find it. - Forgetting that
-fbeatsCOMPOSE_FILE. A leftover environment variable does not interfere with a command that passes-f, but it can mislead you when you read a log. - Passing a file name that does not exist. Compose stops with an error before it changes anything, which is the safe outcome.
Exercise
A server's checkout has compose.yaml, compose.override.yaml, and compose.prod.yaml. An operator runs docker compose -f compose.prod.yaml up -d and gets an error saying the web service has no image or build section. What went wrong, and what command fixes it without loading the development override?
Show answer
compose.prod.yaml is only a patch (restart policy and log level) and has nothing to patch when it is the only file. The base file must come first. Run docker compose -f compose.yaml -f compose.prod.yaml up -d. Because -f is used, compose.override.yaml is not loaded.
Exercise
You export COMPOSE_FILE=compose.yaml:compose.prod.yaml in your shell. You then run docker compose -f compose.yaml -f compose.override.yaml config. Which files does Compose use, and which separator would you need if you were on Windows?
Show answer
Compose uses compose.yaml and compose.override.yaml, because the -f flags take precedence and COMPOSEFILE is ignored. On Windows the default separator in COMPOSEFILE is a semicolon, so the variable would be written compose.yaml;compose.prod.yaml.