Sub topic 3
Adding development settings with compose.override.yaml
This chapter assumes you can read YAML and run docker compose up and docker compose down for a multi-service app. It also assumes you know the basics of images, containers, volumes, and environment variables. By the end, you will be able to keep shared settings in one file and development-only settings in another, and predict what Compose does when both define the same thing.
The problem the override file solves
A base Compose file should describe what every environment shares. That means the service names, the images, the database volume, and the dependencies between services. Development needs more than that. You want to build the image from local source, mount your code into the container, turn on verbose logging, and open a debugger port. Production should not have any of those.
You could keep two complete files and copy changes between them. They would drift apart, and a fix made in one would be missing from the other. Compose avoids this with a convention: if you run docker compose with no -f flag, it reads compose.yaml and then merges compose.override.yaml on top of it, if that file exists.
The base file
The base file holds the shared definition. Note how it handles the database password.
# compose.yaml (base)
services:
web:
image: example/web:1.0
ports:
- "8000:8000"
environment:
LOG_LEVEL: info
depends_on:
- db
db:
image: postgres:16
environment:
POSTGRES_PASSWORD: ${DB_PASSWORD:?set DB_PASSWORD}
volumes:
- db-data:/var/lib/postgresql/data
volumes:
db-data:The expression ${DB_PASSWORD:?set DB_PASSWORD} is variable interpolation with a required value. If DB_PASSWORD is unset or empty, Compose stops with the message set DB_PASSWORD. No secret lives in the file. You can supply the value from your shell or from a .env file next to the Compose file. A later chapter covers interpolation in detail.
On its own, this file runs a pre-built example/web:1.0 image on port 8000. That is close to what production wants, and it is a poor fit for day-to-day coding.
The override file
Create compose.override.yaml in the same directory:
# compose.override.yaml (loaded automatically after compose.yaml)
services:
web:
build: .
volumes:
- ./src:/app/src
environment:
LOG_LEVEL: debug
ports:
- "5678:5678"The override file is a partial Compose file. It only names the services and keys it wants to change or add. It does not repeat image, depends_on, or the whole db service, because those are inherited from the base file.
Now run this from the project directory:
DB_PASSWORD=devpass docker compose up -dCompose finds both files, merges them in order, and starts the result. The effective web service now has:
image: example/web:1.0from the base file. Becausebuild: .is also present, Compose builds from the localDockerfileand tags the result with that image name.build: .from the override. This key did not exist in the base file, so it is added.LOG_LEVEL: debug. Both files set this key, so the later file wins.- Both ports,
8000:8000and5678:5678. Lists such asportsare appended, not replaced. - The
./src:/app/srcbind mount, added to the service. depends_on: [db], untouched.
The db service is unchanged.
How the merge treats each kind of value
The result above follows a few rules. They are covered fully in a later chapter, but you need the basics to write an override file that behaves as you expect.
| Kind of value | Example key | What happens when both files set it |
|---|---|---|
| Single value (scalar) | `image`, `restart`, `command` | The override replaces the base value |
| Mapping | `environment` (map form), `labels` | Keys are merged. Same key: override wins. New keys are added |
| List of items | `ports`, `expose`, `dns` | Items from the override are appended to the base items |
| List with an identity | `volumes` | Entries are merged by container path. An entry with the same target replaces the base entry |
Here is how that plays out for the example:
# Simplified view of the merged web service (not a file you write)
services:
web:
image: example/web:1.0
build: .
ports:
- "8000:8000"
- "5678:5678"
environment:
LOG_LEVEL: debug
volumes:
- ./src:/app/src
depends_on:
- dbThe exact text printed by Compose differs. It expands short syntax, for example ports appear as mappings with target and published fields. The meaning is the same. You will see the real output with docker compose config in a later chapter.
Why this works well for development
Keeping development settings in the default override file has practical benefits.
- Short commands. Developers run
docker compose upand get the development setup without remembering flags. - Small diffs. The override file lists only differences, so a reviewer can see at a glance what makes development different.
- One source of truth. The service names, image tags, and volumes are defined once, in the base file.
- Safe defaults for the base. Because the base has no bind mounts or debug ports, it is a reasonable starting point for other environments.
The last point carries a risk, covered next.
The automatic loading trap
The override file is loaded automatically only when you do not pass -f and COMPOSE_FILE is not set. If you run docker compose up on a production server and the repository's compose.override.yaml is in that directory, the server will run with your debug settings, bind mounts, and exposed debugger port.
You can see this switch in action:
# Development: compose.yaml and compose.override.yaml are both used
docker compose up -d
# Explicit selection: only the listed files are used. The override file is ignored.
docker compose -f compose.yaml -f compose.prod.yaml up -dA later chapter walks through explicit file selection and a production file.
Common mistakes with the override file
- Misspelled or misplaced file name. The file must sit next to the base file and be named
compose.override.yaml(orcompose.override.yml). A name likecompose.dev.yamlis not loaded automatically. You would have to pass it with-f. - Expecting a list to be replaced. Adding
ports: ["9000:8000"]in the override leaves8000:8000in place and adds a second mapping. Both host ports are then open. A later chapter shows the!overridetag for replacing a list. - Duplicating the base. Copying the entire
webservice into the override works, but it defeats the purpose and invites drift. Include only the keys that differ. - Forgetting the required variable. The base file requires
DB_PASSWORD. If you seerequired variable DB_PASSWORD is missing a value: set DB_PASSWORD, set it in the shell or in a.envfile. - Bind mount hides built files. Mounting
./srcover/app/srcreplaces whatever the image contained at that path. If the image built compiled output there, the mount hides it.
Checking your work
You can ask Compose to show the merged result without starting anything:
DB_PASSWORD=devpass docker compose configLook at the web service in the output. Confirm that LOG_LEVEL is debug, that both ports appear, and that the bind mount is present. If you change the override and the output does not change, check the file name and location first.
Exercises
Exercise
You run docker compose up -d in a directory containing compose.yaml and compose.override.yaml. The base file sets LOG_LEVEL: info and ports: ["8000:8000"] for web. The override sets LOG_LEVEL: debug and ports: ["5678:5678"]. What are the final log level and published ports for web?
Show answer
The log level is debug, because the override replaces the value for the same key in the environment mapping. The published ports are both 8000:8000 and 5678:5678, because list items such as ports are appended instead of replaced.
Exercise
A teammate deploys the repository to a server and runs docker compose up -d. The server then exposes a debugger port. Why did this happen, and what is one way to prevent it?
Show answer
With no -f flag and no COMPOSE_FILE variable, Compose automatically loads compose.override.yaml after compose.yaml, so the development settings, including the debugger port, were applied. To prevent it, run docker compose -f compose.yaml -f compose.prod.yaml up -d so the override is not loaded, or do not copy the override file to the server.