brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Guide: make Docker Compose wait until the database is really ready, not just started

make Docker Compose wait until the database is really ready, not just started

Guide docker

5 steps 3 min read

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.

Terminal output of docker compose up -d: the network is created, the db container becomes Healthy after 16.5 seconds and only then the web container starts
docker compose up -d on a test stack. web is only started once db reports Healthy.

Step 3 of 5: Check that it works

docker compose up -d
docker compose ps
Terminal output of docker compose ps right after starting the db service, with status Up 3 seconds (health: starting)
Right after the start, docker compose ps shows the database as (health: starting).
Terminal output of docker compose ps showing the db service as healthy, followed by docker compose exec db pg_isready answering accepting connections
Twenty seconds later the status is (healthy). pg_isready by hand answers the same way the check does.

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.

Terminal output of docker inspect piped through jq showing Status healthy, FailingStreak 0 and the last check with exit code 0 and the output accepting connections
The health state of the container, trimmed to the last check with jq: exit code 0, healthy.

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

Link