Sub topic 13
Production and CI practices
This chapter assumes you know how -f and COMPOSE_FILE select files, how Compose merges them, and how docker compose config and profiles work. It turns those pieces into habits for production and CI.
Keep the base file neutral
The base file should hold only what every environment shares. It should not hold development conveniences such as bind-mounted source code or debugger ports. It should not hold secrets either. The base file from this tutorial requires DB_PASSWORD with ${DB_PASSWORD:?set DB_PASSWORD}, so a missing secret stops Compose before any container starts.
Put each environment's differences in its own file:
| Environment | Files | Typical purpose |
|---|---|---|
| Development | `compose.yaml` + `compose.override.yaml` (automatic) | Local build, bind mounts, debug logging |
| Test and CI | `compose.yaml` + `compose.test.yaml` | Run the test command, exit with its result |
| Production | `compose.yaml` + `compose.prod.yaml` | Restart policies, quiet logs, pinned images |
Running tests in CI
A CI run should start from a clean state and report the test result as an exit code. This test file replaces the web command with the test runner:
# compose.test.yaml
services:
web:
build: .
command: ["python", "-m", "pytest"]The script below runs it:
#!/usr/bin/env bash
set -euo pipefail
export COMPOSE_FILE=compose.yaml:compose.test.yaml
export COMPOSE_PROJECT_NAME="ci-${CI_JOB_ID:?set CI_JOB_ID}"
export DB_PASSWORD="${DB_PASSWORD:?set DB_PASSWORD}"
trap 'docker compose down --volumes' EXIT
docker compose config --quiet
docker compose up --build --exit-code-from webEach line does one job:
COMPOSE_PROJECT_NAMEgives every job its own project name. Two jobs on one runner then never share containers, networks, or volumes.docker compose config --quietprints nothing. It fails if the merged file is invalid or a required variable is missing, so mistakes surface before the build.--exit-code-from webstops the stack whenwebexits and returns its exit code. The CI job passes or fails with the tests.- The
trapremoves containers and volumes even when the tests fail.
The base file's depends_on only orders startup. It does not wait for Postgres to accept connections. If tests fail intermittently on the first connection, add a healthcheck to db and use depends_on with condition: service_healthy.
Deploying with an explicit file list
In production, always name every file. Never rely on the automatic override file.
docker compose -f compose.yaml -f compose.prod.yaml config --quiet
docker compose -f compose.yaml -f compose.prod.yaml pull
docker compose -f compose.yaml -f compose.prod.yaml up -d --wait
docker compose -f compose.yaml -f compose.prod.yaml run --rm migratepull fetches the pinned images. up -d --wait returns once services are running or healthy. run --rm migrate starts the tools profile service by naming it, so migrations run only when you ask for them.
A checklist
- Pin image tags such as
example/web:1.0. Do not uselatest. - Pass secrets through the environment or an env file that is not in version control. Keep the
:?guard so a missing secret fails fast. - Leave profiles such as
debugoff in production. Enable them only for a specific task. - Run
docker compose configin CI with the production file list. Review its output when the file changes. - Use a unique project name per CI job and remove volumes at the end.
Exercise
A teammate deploys with plain docker compose up -d on the production server, and the app starts with debug logging and an exposed port 5678. What happened, and what is the fix?
Show answer
With no -f flag and no COMPOSE_FILE, Compose loaded compose.yaml plus the automatic compose.override.yaml, which sets LOG_LEVEL: debug and publishes port 5678. The fix is to deploy with an explicit list, docker compose -f compose.yaml -f compose.prod.yaml up -d, or to set COMPOSE_FILE=compose.yaml:compose.prod.yaml. Also keep the override file off the production host.