Sub topic 13
Production and CI practices
This chapter assumes you can read a base Compose file and a compose.override.yaml. It also assumes you know how -f selects files and how docker compose config prints the merged result. Here you turn those pieces into a project layout that runs the same way on a laptop, in a CI job, and on a server.
One rule that prevents most mistakes
Development should be the convenient default, and production and CI should be explicit. Compose reads compose.override.yaml automatically only when you pass no -f flag and COMPOSE_FILE is unset. Production and CI commands should therefore always name their files. Then a developer's local override can never leak into a deployment.
| Environment | Files | How they are selected |
|---|---|---|
| Development | `compose.yaml` + `compose.override.yaml` | Automatic, so `docker compose up` is enough |
| Test (CI) | `compose.yaml` + `compose.test.yaml` | `-f` flags in the CI script |
| Production | `compose.yaml` + `compose.prod.yaml` | `-f` flags in the deploy script |
Keep the base file environment-neutral
The base file holds only what is true everywhere: service names, images, networks, volumes, and required variables. It does not hold debug ports, source mounts, or restart policies. If you cannot imagine a setting being right in all three environments, move it to an environment file.
The base file also declares which values must be provided. The ${DB_PASSWORD:?set DB_PASSWORD} form fails the command with your message when the variable is empty or unset. A missing secret then stops the deployment instead of starting a database with a blank 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:A production file
Production should run a built, versioned image. It should not build from source or mount code. Because compose.prod.yaml is never combined with the development override, build: . and the ./src mount cannot appear.
The production file below pins the image through a required TAG variable. Merging replaces a scalar such as image, so this value wins over the base.
# compose.prod.yaml
services:
web:
image: example/web:${TAG:?set TAG to a release tag}
restart: unless-stopped
environment:
LOG_LEVEL: warning
db:
restart: unless-stoppedSome practices that go with this file:
- Deploy an immutable tag such as a version number or a commit hash. Avoid
latest, because you cannot tell later what was running. - Do not publish the database port in production. If the base file does not publish it, nothing needs removing.
- Put restart policies in the production file only. On a laptop, a container that restarts forever hides crashes.
- Keep the debug port (5678 in the override file) out of every file except the development one.
A typical deploy runs three commands. The first validates, the second fetches images, and the third starts services and waits for them:
# Values come from an explicit file, not from a stray .env in the working directory
export COMPOSE_FILE=compose.yaml:compose.prod.yaml
docker compose --env-file /etc/myapp/prod.env config --quiet
docker compose --env-file /etc/myapp/prod.env pull
docker compose --env-file /etc/myapp/prod.env up -d --wait --remove-orphansconfig --quietprints nothing and exits non-zero if the files do not parse, a required variable is missing, or a reference is invalid.--env-filereplaces the default.envlookup. This matters because a.envfile in the project directory is often a developer's file, with development values.--waitreturns only when services are running and healthy, so the script can fail when a service does not come up.--remove-orphansremoves containers for services that you deleted from the files.
A test file for CI
A CI run needs three things: a way to run the tests, a way to know the database is ready, and a way to clean up. The test file below adds a tests service and a health check on the database. It uses the long form of depends_on, which supports condition: service_healthy.
# compose.test.yaml
services:
db:
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 2s
timeout: 3s
retries: 15
tests:
build: .
command: ["python", "-m", "pytest", "-q"]
environment:
LOG_LEVEL: warning
depends_on:
db:
condition: service_healthyThe CI script selects the files, uses a unique project name, and removes everything when it finishes, whether or not the tests passed:
#!/usr/bin/env bash
set -euo pipefail
export COMPOSE_FILE=compose.yaml:compose.test.yaml
export COMPOSE_PROJECT_NAME="myapp-ci-${CI_JOB_ID:-local}"
export DB_PASSWORD="ci-only-password"
cleanup() {
docker compose down --volumes --remove-orphans
}
trap cleanup EXIT
docker compose config --quiet
docker compose up --build --exit-code-from testsEach choice in this script has a reason:
COMPOSE_PROJECT_NAMEis unique per job. Compose prefixes container, network, and volume names with the project name. Two jobs on one runner would otherwise share names and interfere with each other.--exit-code-from testsstops all services when thetestscontainer exits and returns its exit code. The CI job then passes or fails with the tests.trap cleanup EXITrunsdown --volumeseven when the tests fail. Without--volumes, thedb-datavolume survives and the next run starts with old data.- The password is a throwaway value that exists only for the job. Real secrets come from your CI system's secret store, not from the script.
Using profiles in production and CI
Profiles suit tools that are part of the project but should not run all the time. Examples are a database admin page and a migration step. In the earlier chapter, adminer is in the debug profile and migrate is in the tools profile.
Keep profile selection visible in the command:
- In production, run the one-off job by naming it:
docker compose -f compose.yaml -f compose.prod.yaml run --rm migrate. Naming a profiled service activates its profile for that command only. - Do not set
COMPOSE_PROFILESin a shared.envfile. Every command in that directory, including production ones, would then start the debug tools. - In CI, activate profiles only for the jobs that need them, for example
--profile toolsin a migration check job.
Check the result in the pipeline
Merging errors are quiet. A misspelled service name in compose.prod.yaml, for example wbe:, does not fail the merge. Compose creates a new service called wbe with no image. Catch this before deploy with checks like these:
# Every environment combination must parse and resolve
DB_PASSWORD=x TAG=test docker compose -f compose.yaml -f compose.prod.yaml config --quiet
DB_PASSWORD=x docker compose -f compose.yaml -f compose.test.yaml config --quiet
DB_PASSWORD=x docker compose config --quiet
# The service list for production should be exactly what you expect
DB_PASSWORD=x TAG=test docker compose -f compose.yaml -f compose.prod.yaml config --servicesA stricter pipeline compares the output of config --services against a committed list. A typo then shows up as a changed service list in review.
Common failures
| Symptom | Likely cause | Fix |
|---|---|---|
| Production starts with debug logging or a source mount | `-f` was omitted, so the development override loaded | Set `COMPOSE_FILE` or pass `-f` in the deploy script |
| `required variable TAG is missing a value` | The env file was not passed, or does not define it | Pass `--env-file` and check the file path |
| CI job sees data from an earlier run | No `down --volumes`, or a reused project name | Use a unique project name and a cleanup trap |
| Tests start before the database accepts connections | `depends_on` without `condition: service_healthy` | Add a health check and the long form of `depends_on` |
| Debug tools run in production | `COMPOSE_PROFILES` set in a shared `.env` | Remove it and pass `--profile` explicitly |
Exercise
A teammate runs docker compose up -d on the production server, from a directory that contains compose.yaml, compose.override.yaml, and compose.prod.yaml. What does Compose load, and what two changes would prevent this from being possible?
Show answer
Compose loads compose.yaml and compose.override.yaml, because no -f flag or COMPOSEFILE is set. compose.prod.yaml is ignored, so the server runs the development build, source mounts, and debug port. To prevent this, set COMPOSEFILE=compose.yaml:compose.prod.yaml in the server's environment or deploy script, or always pass -f compose.yaml -f compose.prod.yaml. Removing compose.override.yaml from the deployed directory also works, since production never needs it.
Exercise
Your CI script ends with docker compose up --build --exit-code-from tests and no cleanup step. Two jobs run on the same runner. Name two problems this can cause and one fix for each.
Show answer
First, without a unique COMPOSEPROJECTNAME the jobs share container, network, and volume names and can interfere with each other. Fix it by setting COMPOSEPROJECTNAME to include the job ID. Second, without cleanup the db-data volume and the containers stay behind, so a later run may start with old data. Fix it with a trap that runs docker compose down --volumes --remove-orphans on EXIT.