Everyone has seen an app container crash on boot with “connection refused” because its database was still starting. Plain depends_on does not fix that: it only waits until the other container has started, not until it is ready. The fix is a healthcheck on the database plus condition: service_healthy on the app. I would add it to every stack with a database, since it takes ten lines. Needs Docker Compose v2; I use PostgreSQL as the example.
- Time
- 3 min
- Steps
- 5
- Updated
- 19 Nov 2026
You will learn
- The healthcheck
- Make the app wait for it
- Check that it works
- Where it goes wrong
- What it does not do
Step 1 of 5: The healthcheck
A healthcheck is a command Docker runs inside the container every few seconds. Exit code 0 means healthy. PostgreSQL ships a tool for exactly this, pg_isready:
services:
db:
image: postgres:17
environment:
POSTGRES_USER: app
POSTGRES_PASSWORD: change-me
POSTGRES_DB: app
healthcheck:
test: ["CMD-SHELL", "pg_isready -U $${POSTGRES_USER} -d $${POSTGRES_DB}"]
interval: 5s
timeout: 3s
retries: 10
start_period: 20s
The doubled $$ is on purpose: Compose would otherwise fill in a variable from your own shell, and you want the one inside the container. interval is how often it checks, retries is how many failures in a row make it unhealthy, and start_period gives a slow first boot some room, since failures in that window do not count against the retries. The docs also list start_interval for faster checks during that window, available from Compose 2.20.2.
Step 2 of 5: Make the app wait for it
web:
image: your/app:latest
depends_on:
db:
condition: service_healthy
Now docker compose up -d starts the database first and only creates web once db reports healthy. The Compose reference describes service_healthy as exactly that: the dependency is expected to be healthy, as indicated by its healthcheck, before the dependent service starts.

Step 3 of 5: Check that it works
docker compose up -d
docker compose ps


The status column shows starting first and then healthy. If you want the details of the last checks, including the output of the command:
docker inspect --format '{{json .State.Health}}'
Replace the placeholder with the name from docker compose ps. Pipe it through jq if you want it readable.

Step 4 of 5: Where it goes wrong
A healthcheck that can never pass leaves the app waiting forever, so test the command by hand first with docker compose exec db pg_isready -U app. The tool has to exist inside the image: pg_isready is in the official Postgres image, but a slim image of another database may not have a client, and then you need a different command. Other images have their own habit (MariaDB has a healthcheck script, Redis answers redis-cli ping); check the image’s documentation instead of guessing a flag.
Step 5 of 5: What it does not do
This only controls start order. If the database dies an hour later, Compose does not restart the app because of it. For that you still want restart: unless-stopped on the app and an app that retries its connection. I see the healthcheck as the polite way to start a stack. It does not replace monitoring.
Further reading
- Docker Compose services reference (depends_on, healthcheck)
- PostgreSQL pg_isready
- On this site: Keep passwords out of your Docker Compose file
- On this site: Stop Docker logs from filling your disk with log rotation
- On this site: Docker Compose healthchecks, and how to see why a container is unhealthy
