How to Add Compose Healthchecks and Gate CI on Them
A healthcheck turns "container started" into "service is actually accepting connections", which is what CI needs before it runs tests.
Add a healthcheck: with a real readiness probe (for example pg_isready or an HTTP curl), then let dependents use depends_on: condition: service_healthy or gate the whole run with --wait.
Steps
- Pick a probe that fails until the service accepts real traffic.
- Tune
interval,timeout,retries, andstart_period. - Reference it via
depends_onwithcondition: service_healthy.
Healthcheck
docker-compose.yml
services:
db:
image: postgres:16
environment:
POSTGRES_PASSWORD: test
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 5s
timeout: 3s
retries: 10
start_period: 10s
app:
build: .
depends_on:
db:
condition: service_healthyGotchas
start_periodfailures do not count againstretries, giving slow starters room to boot.- Use
CMD-SHELLwhen the probe needs shell features like variable expansion or pipes.
Related guides
How to Run docker compose up and Wait for Healthy Services in CIStart a Compose stack in CI with docker compose up -d and block until every service is healthy using the --wa…
How to Provide a Postgres Fixture With Compose in CIStand up a disposable Postgres for CI tests with docker compose, seed it via an init script, and gate the tes…
How to Debug a service unhealthy Error in CIFix "dependency failed to start: container is unhealthy" in Compose CI by inspecting the healthcheck output,…