Docker Compose Healthchecks: Stop Your Containers Restarting in a Loop

A container that keeps restarting burns CPU, floods your logs, and never serves a single request. The usual cause is not a broken image. It is a container that starts before its database is ready, or a probe that asks the wrong question. Docker compose healthchecks fix the first problem, and a correct restart policy keeps the second one from turning into an endless loop.

This guide shows how healthchecks work in Compose, how to write one that tests what actually matters, and how to break out of a restart loop when one is already running.

Why containers restart in a loop

Docker starts containers in order, but starting is not the same as being ready. A web app can be up in two seconds while its database still needs twenty. The app tries to connect, fails, exits, and Docker starts it again. That is the classic docker container restart loop fix scenario, and it looks like a crash even though nothing is wrong with the code.

A second cause is a bad health probe. A check that curls the wrong path, hits a port the app does not listen on, or runs before the app has had time to boot marks a healthy container as sick. Docker then kills and restarts it, and the cycle repeats. A third cause is a real crash on boot: a missing environment variable, a bad config file, or a permission error on a mounted volume. Healthchecks cannot fix that one. They only tell you the container is sick instead of letting it fail silently.

How Docker Compose healthchecks work

A healthcheck is a command Docker runs inside the container on a schedule. If the command exits 0, the container is healthy. Any other exit code means unhealthy. Compose supports test, interval, timeout, retries, and start_period. The start_period is the grace window: Docker does not count failures during it, which is what you want for a slow app that needs a minute to warm up.

Here is a healthcheck on a web service that waits for a real HTTP response:

services:
  web:
    image: myapp:latest
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:3000/health"]
      interval: 30s
      timeout: 5s
      retries: 3
      start_period: 40s

Use an endpoint that tests something real. A /health route that checks the database connection tells you far more than a check that only proves the process is alive. If your app has no health endpoint, curl -f http://localhost:3000/ is a fair start, but add a real endpoint when you can. If curl is not in the image, use wget -q -O /dev/null http://localhost:3000/health or a small script in the image instead of installing tools at runtime.

Gate startup with depends_on and condition service_healthy

A healthcheck on its own does not stop the app from starting too early. You need the docker healthcheck depends_on condition to hold the app back until the database reports healthy:

services:
  web:
    image: myapp:latest
    depends_on:
      db:
        condition: service_healthy
    restart: unless-stopped

  db:
    image: postgres:16
    environment:
      - POSTGRES_PASSWORD=change-this-password
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres"]
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 10s

With this in place, Compose waits until Postgres answers pg_isready before it starts the web container. The same pattern works for MySQL with mysqladmin ping and for Redis with redis-cli ping. This one change ends most restart loops in multi-container stacks. If you want the wider picture on container health, read our guide to setting up health checks for Docker containers on a VPS. For a first look at how Compose files fit together, our Docker Compose explainer covers the basics.

Pick the right restart policy

The docker compose restart policy decides what happens after a container exits. Four options exist, and picking the wrong one is how a crash becomes a loop:

  • no: never restart. Good for one-off jobs and migrations.
  • always: restart no matter how the container stopped, even after you stop it by hand and reboot the host. Useful for services that must stay up, but it restarts a container that exits with an error every time.
  • unless-stopped: like always, but it stays stopped if you stopped it yourself. This is the right default for most long-running services.
  • on-failure: restart only when the container exits with a non-zero code, with an optional limit like on-failure:5. This is the right pick when you want a bounded number of retries instead of an endless loop.

For a web app backed by a healthy database, use unless-stopped. For a worker that should try a few times and then stay down so you can read the logs, use on-failure:5. Never use always on a container you have not watched start cleanly at least once. It will hide the failure and fill the disk with logs while you look elsewhere. If logs are already eating your disk, our guide to Docker log management shows how to cap them.

Break out of a restart loop that is already running

When a container is looping, stop it first so you can read what it says:

docker compose stop web
docker compose logs --tail 100 web

Read the last lines before each restart. A connection refused error points at a missing or unready dependency. A missing variable or file error points at your Compose file or volume. A port error means something else already holds that port, and our guide to fixing port already in use errors in Docker walks through that case.

Once you know the cause, fix it and start the container by hand to watch it boot:

docker compose up web

Without the -d flag, the logs stream to your terminal and you see the failure as it happens. Press Ctrl+C when you have seen enough, then start it detached with docker compose up -d web. Check the health state with docker ps, which shows healthy, starting, or unhealthy next to the container name.

Healthcheck mistakes that cause loops

  • Probing the wrong port. The check must hit the port the app listens on inside the container, not the port mapped on the host.
  • No start_period on a slow app. A Java or Node app that needs 60 seconds to boot fails three quick checks and gets killed before it ever finishes starting.
  • A check that needs a tool the image lacks. Alpine images often ship without curl. Test the command with docker exec before you trust it.
  • Interval shorter than the timeout. A 5 second interval with a 10 second timeout stacks checks on top of each other. Keep the interval longer than the timeout.
  • Checking only the process. A test: ["CMD", "true"] check always passes and tells you nothing. Probe a real endpoint or the real dependency.

Add healthchecks to every service that another service depends on, give slow apps a real start_period, and pair the checks with unless-stopped or a bounded on-failure policy. A stack built that way starts in the right order, waits for what it needs, and tells you plainly when something is wrong instead of spinning in silence.

Ditulis oleh

Firdaus Safari Putra

Founder, PT FirKaizen Digital Solutions

Firdaus Safari Putra adalah founder PT FirKaizen Digital Solutions. Ia membangun dan mengoperasikan infrastruktur server di balik panduan ini, dan menulis berdasarkan apa yang benar-benar ia jalankan, uji, dan ukur — bukan teori.