Sub topic 3
Adding development settings with compose.override.yaml
This chapter assumes you can read Compose YAML, run docker compose up, and know what images, volumes, and environment variables are. It also assumes you have the base compose.yaml from the previous chapter, which is repeated below.
How the override file works
When you run docker compose up with no -f flag, Compose looks in the current directory for two files:
compose.yaml, the base file.compose.override.yaml, which is merged over the base if it exists.
The override file needs no special syntax. It is a normal Compose file that lists only the settings you want to add or change. Because it is picked up automatically, it is a good place for settings that only developers need.
The base file
The base file holds what every environment shares. It requires DB_PASSWORD to be set instead of hard-coding a secret.
# 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 development override
This file builds the image locally, mounts your source code, raises log verbosity, and exposes a debugger port.
# compose.override.yaml (loaded automatically after compose.yaml)
services:
web:
build: .
volumes:
- ./src:/app/src
environment:
LOG_LEVEL: debug
ports:
- "5678:5678"Start the stack:
DB_PASSWORD=devpass docker compose up -dFor web, Compose combines the two files like this:
| Setting | Base | Override | Result |
|---|---|---|---|
| `image` | `example/web:1.0` | none | kept |
| `build` | none | `.` | added |
| `LOG_LEVEL` | `info` | `debug` | `debug` |
| `ports` | `8000:8000` | `5678:5678` | both published |
| `volumes` | none | `./src:/app/src` | added |
When a service has both image and build, Compose builds the image from the build context and tags it with the image name. The service name, web, stays the same, so depends_on and networking keep working.
Common errors
- Wrong file name. Compose only finds
compose.override.yaml(orcompose.override.yml), and the olderdocker-compose.override.ymlfor legacy projects. A name likecompose.dev.yamlis ignored unless you pass it with-f. - Expecting the override in production. The override loads only when you do not use
-forCOMPOSE_FILE. Choosing files explicitly is covered in the next chapter. - Trying to remove a base port. Lists such as
portsandvolumesare appended, not replaced. The override above cannot drop8000:8000. Later chapters show how to replace inherited values. - Unset variables. If
DB_PASSWORDis missing,docker compose upstops with the messageset DB_PASSWORD. This is intended.
Exercise
A teammate adds compose.override.yaml with LOG_LEVEL: debug. In CI, the job runs docker compose -f compose.yaml up -d and logs stay at info. Why?
Show answer
Passing -f turns off the automatic loading of compose.override.yaml. Compose reads only the files you list, so the base value info stays. To include the override in CI, list it explicitly with -f compose.override.yaml.