Sub topic 2
Why Development, Test, and Production Need Different Settings
This chapter assumes you can read YAML, know what images, containers, and volumes are, and have run docker compose up and docker compose down for a multi-service app. It explains why a single Compose file rarely fits every environment, and it sets up the problem that override files and profiles solve in later chapters.
One application, three situations
The same application runs in at least three situations, and each one has different needs.
| Concern | Development | Test (CI) | Production |
|---|---|---|---|
| Image source | Built locally from source | Built or pulled for the commit under test | Pulled from a registry at a fixed tag |
| Source code | Mounted from the host for live reload | Baked into the image | Baked into the image |
| Log level | `debug` | `info` | `warning` |
| Restart policy | None | None | `unless-stopped` |
| Extra ports | Debugger, database admin UI | Usually none | Only the public port |
| Secrets | Throwaway values | Injected by the CI system | Injected by a secret store |
| Optional tools | Database browser, migration runner | Test runner | Rarely any |
None of these differences changes what the application is. The web and db services, their images, and the shared volume stay the same. Only a few attributes change per environment.
The cost of one file per environment
The first idea is often to copy compose.yaml into compose.dev.yaml, compose.test.yaml, and compose.prod.yaml, then edit each copy. This works for a week. Then someone changes the database image in one copy and forgets the others. Staging now runs a different Postgres major version than production, and the difference only shows up as a bug.
Duplicated files drift apart. The goal is the opposite: define what is shared once, and describe each environment as a small set of differences.
A shared base
The base file below holds what every environment needs. It runs web and db, stores data in a named volume, and sets a default log level.
# 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:Two details matter here.
- The
webservice uses a fixed image tag,example/web:1.0. Development will later replace it with a local build, but production keeps the tag. - The password is not written in the file. The expression
${DB_PASSWORD:?set DB_PASSWORD}makes Compose stop with the messageset DB_PASSWORDif the variable is unset or empty. A missing secret fails loudly instead of starting a database with a blank or default password.
Two tools for the differences
Compose offers two mechanisms for expressing per-environment differences. They solve different problems.
- Override files change the settings of services that already exist. For example, development adds a bind mount and raises
LOG_LEVEL, and production adds a restart policy. Compose merges the extra files over the base. - Profiles decide whether a service runs at all. A database browser belongs in development only, so you tag it with a profile and start it on request.
A useful test is to ask what changes. If a service stays but its settings differ, use an override. If a service should exist only sometimes, use a profile. Later chapters cover both in detail and show how to combine them.
Where the differences should not live
Some differences should stay out of Compose files entirely. Secret values, hostnames, and similar per-machine data belong in environment variables or env files, which Compose substitutes into the file at run time. The base file above already follows this rule with DB_PASSWORD. A later chapter covers variable interpolation and env files.
Exercise
Exercise
A team keeps compose.dev.yaml and compose.prod.yaml as two full copies of the same services. They also want a database browser that runs only on developer machines. For each need, say whether an override file or a profile fits better, and why.
Show answer
Replacing the two full copies with a shared base plus a small override per environment fits the first need. Both environments run the same services and differ only in a few settings, so overrides remove the duplication and prevent drift. The database browser fits a profile. It is a whole service that should exist only sometimes, so tagging it with a profile keeps it out of production by default and lets developers start it on request.