Sub topic 2
Why Development, Test, and Production Need Different Settings
This chapter assumes you can read basic YAML and have run docker compose up and docker compose down on a multi-service app. It also assumes you know what images, containers, and volumes are. You don't need to know how Compose merges files yet. Later chapters cover that. This one explains the problem those tools solve.
One application, three environments
An environment is a place where your application runs for a particular purpose. Most projects have at least three:
- Development is your laptop. You change code constantly and want fast feedback and detailed logs.
- Test is usually a CI runner or a throwaway stack. You want a clean, repeatable run that you can create and destroy many times.
- Production is the real deployment. You want stability, restricted access, and quiet logs.
The application code is the same in all three. The way it is built, started, exposed, and supplied with secrets is not. A single Compose file that tries to describe all three at once ends up holding settings that are wrong or unsafe in at least two of them.
What actually changes
The table shows typical differences for a web service backed by a database.
| Concern | Development | Test | Production |
|---|---|---|---|
| Where the code comes from | Built locally, source mounted | Image built by CI | Pinned image from a registry |
| Log level | `debug` | `info` | `warning` |
| Published ports | App port plus a debugger port | None, or random host ports | Only the public app port |
| Restart policy | None | None | `unless-stopped` |
| Database password | Throwaway local value | CI secret | Injected from a secret store |
| Database data | Persistent named volume | Disposable | Persistent and backed up |
| Extra services | Database browser, mail catcher | Test runner, fixtures | None |
Each row is a decision, and each is worth understanding on its own.
Where the code comes from
In development you want an edit on your laptop to show up in the running container immediately. That means a build: section and a bind mount of your source directory. In production you want the opposite. The container should run exactly the image you tested, identified by a fixed tag or digest, with no dependence on files on the host.
How the container behaves when things go wrong
During development a crashed container is useful information. You want it to stop so you can read the error. In production the same crash should trigger a restart. That is the job of the restart attribute. A restart policy on your laptop can hide a crash loop, and a missing one on a server turns a brief fault into an outage.
What is exposed
A debugger port is helpful locally and a security hole on a server. A database port published to the host is convenient for a GUI client on your laptop and unnecessary in production. There the database should be reachable only from other services on the Compose network.
Secrets and configuration
Production secrets must not be committed to the repository, and neither should the file that carries them. Development can safely use a throwaway password. Test usually takes its values from the CI system. These three sources differ, but the service definition that reads the value, such as POSTGRES_PASSWORD, stays the same.
Optional services
Some services are only useful in one context. A database browser helps during development. A migration job or a test runner should run only on demand. If they sit in the main service list, they start every time and run in production where nobody wants them.
What goes wrong with one file for everything
Suppose you write one file that works well on your laptop:
# compose.yaml (a file that only suits development)
services:
web:
build: .
ports:
- "8000:8000"
- "5678:5678"
volumes:
- ./src:/app/src
environment:
LOG_LEVEL: debug
db:
image: postgres:16
ports:
- "5432:5432"
environment:
POSTGRES_PASSWORD: devpasswordRun it on a production server and several things go wrong at once:
- The bind mount
./src:/app/srcuses a path relative to the Compose file. If that directory does not exist on the server, Docker creates an empty one. The empty directory then hides the application code that was baked into the image, so the container starts with no code. - Port 5678 exposes a debugger to anyone who can reach the host.
- Port 5432 exposes the database, protected only by a password that is written in the file.
build: .forces a build on the server, so the running image may differ from the one you tested.LOG_LEVEL: debugwrites request details to logs, which can include data you did not intend to store.- There is no restart policy, so a crash leaves the service down.
None of these is a bug in Compose. The file is correct for the environment it was written for.
Test runs have a different failure. Two CI jobs on the same runner both try to publish host port 8000, and the second one fails to start. Publishing only the container port ("8000") makes Docker choose a free host port. Running each job under its own project name with -p (or COMPOSE_PROJECT_NAME) also keeps container and volume names from colliding.
Three ways to handle the differences
There are three common approaches. Only the last is worth using for anything beyond a quick experiment.
- Separate full copies. You keep
compose.dev.yamlandcompose.prod.yaml, each complete. This is easy to understand, but the copies drift. Someone adds an environment variable to one file and forgets the other, and production breaks on a setting that "worked in development". - Editing or commenting out lines. You change the file before each deployment. This is error-prone and leaves the working tree dirty. Nobody can tell what was actually deployed.
- A shared base plus small layers. One file holds what is true everywhere. Each environment adds only its differences. A change to the shared part is made once, and the environment files stay short enough to review at a glance.
Compose supports the third approach in two complementary ways:
- Override files change or add settings on top of a base. A later file wins over an earlier one where they conflict.
- Profiles mark services that should start only when you ask for them.
Variable interpolation, which fills ${NAME} placeholders from your shell or an env file, covers values that differ without changing the structure.
Deciding what belongs where
Use these three questions for each setting.
- Is it true in every environment? Put it in the base. Examples are the image name for the database, the internal service names, and the named volume that stores the database data.
- Does it change the shape of a service in only some environments? Put it in an override file. Examples are
build:, source mounts, extra ports, and restart policies. - Is it a whole service needed only sometimes? Give it a profile. Examples are a database browser or a migration job.
A value that differs but does not change structure, such as a password or an image tag, is usually better as a variable than as an override. That keeps the files free of secrets and lets the same file work with different inputs.
A good base also fails loudly when a required value is missing. The shared database definition can use ${DB_PASSWORD:?set DB_PASSWORD}. Compose then stops with a clear message if the variable is empty or unset, instead of starting a database with a blank password.
# Shared part of the base: required secret, no default
services:
db:
image: postgres:16
environment:
POSTGRES_PASSWORD: ${DB_PASSWORD:?set DB_PASSWORD}Do not put real production secrets in any file you commit. Interpolation lets you supply them at deploy time instead.
Edge cases to keep in mind
- A layer can only change what the base already defines, or add new things. If the base hard-codes something that production must not have, such as a published database port, an override may not be able to remove it cleanly. Keep risky settings out of the base. Later chapters show the merge rules and the
!resetand!overridetags for the cases where you must remove something. - Test should resemble production more than development. If your test layer mounts source code and uses
build:while production uses a pinned image, tests can pass on code that production never runs. Make the test layer closer to production, with changes only for isolation, such as no fixed host ports. - Development conveniences should be opt-in or automatic only on your laptop. Compose reads
compose.override.yamlautomatically. That is convenient on a laptop and a hazard on a server, where a stray override file silently changes behavior. The next chapter covers it directly.
Exercise
A teammate says, "Our compose file works on my laptop, so we can copy it to the production server unchanged." Give three specific settings from a typical development file that would make this a bad idea, and say what each would cause.
Show answer
Any three of these. A bind mount such as ./src:/app/src can hide the code in the image with an empty or wrong directory on the server. A published debugger port (for example 5678) exposes a debugging interface to the network. A published database port (5432) with a password stored in the file exposes the database. build: . makes the server build its own image, which may differ from the tested one. LOG_LEVEL: debug can write sensitive data to logs. A missing restart policy leaves the service down after a crash.
Exercise
For each item, say whether it belongs in the base file, an environment override, or a profile: (a) the postgres:16 image and its named data volume, (b) restart: unless-stopped, (c) a database browser service used only when debugging, (d) the database password.
Show answer
(a) Base, because every environment needs it. (b) Production override, because it changes how a service behaves in only one environment. (c) Profile, because it is a whole service needed only on demand. (d) A variable, such as ${DB_PASSWORD:?set DB_PASSWORD} in the base, with the value supplied by the environment or an env file that is not committed.