Docker Compose Healthchecks: Wait for Ready in 5 Steps

Template cover for Docker Compose healthchecks showing depends_on with condition service_healthy.

Use a healthcheck on a dependency and depends_on: condition: service_healthy on its consumer. Plain depends_on orders startup; it does not prove the dependency can answer requests.

This disposable demo uses a Python HTTP server and a one-shot client. It publishes no host ports, needs no database password, and uses Python’s standard library instead of assuming curl exists in the image. The same startup distinction matters when an AI workflow depends on a local API.

Verification, November 30, 2026: the Python probe was tested locally on Python 3.11.2: HTTP 200 exited 0, HTTP 503 exited 1, and connection refusal exited 1. Docker is unavailable in the editorial environment, so the Compose stack, image pull, service DNS and startup gate have not been tested end to end. Run the checks below before using this configuration beyond a local demo.

1. Start in a separate demo directory

mkdir compose-readiness-demo
cd compose-readiness-demo
docker compose version

You need Docker Engine or Docker Desktop running and the modern docker compose command. Use an empty directory so these commands cannot operate on an existing application stack.

2. Give the server a check it can actually run

Save this as compose.yaml:

services:
  api:
    image: python:3.11-slim
    working_dir: /tmp
    command: ["python", "-m", "http.server", "8000"]
    healthcheck:
      test:
        - CMD
        - python
        - -c
        - "import urllib.request; urllib.request.urlopen('http://127.0.0.1:8000/', timeout=2).close()"
      interval: 5s
      timeout: 3s
      retries: 5
      start_period: 10s

  client:
    image: python:3.11-slim
    depends_on:
      api:
        condition: service_healthy
    command:
      - python
      - -c
      - "import urllib.request; print(urllib.request.urlopen('http://api:8000/', timeout=2).status)"

The server’s check connects to 127.0.0.1 because it runs inside the server container. The client uses api, the Compose service name, to reach the other container. Confusing those two addresses is a common way to write a probe that checks the wrong process.

CMD runs the argument list directly, without shell operators. The two-second HTTP timeout is shorter than Docker’s three-second probe timeout. These timings are demo settings, not measured production recommendations. The image tag selects the Python 3.11 slim family; it is not a pinned digest or a claim about the latest Python release.

3. Validate the file and start the demo

docker compose config --quiet
docker compose up -d
docker compose ps -a
docker compose logs --no-color client

Configuration validation catches Compose syntax errors; it does not test image availability, networking or readiness. In a successful run, the API becomes healthy and the client prints 200. That is expected output, not an end-to-end result observed by this article.

The client then exits because it performs one request. An exited client is not automatically an error: check its exit code and logs. For a real long-running worker, replace the client command with the worker’s normal entry point and keep the dependency condition.

4. Inspect the probe before changing retry counts

docker compose exec api python -c "import urllib.request; urllib.request.urlopen('http://127.0.0.1:8000/', timeout=2).close()"
docker compose ps -a
docker compose logs --tail=50 api

The manual probe is quiet on success. A traceback or nonzero exit means you should check the listener, port and endpoint before adding a longer delay.

  • Executable missing: a curl probe cannot work if the container does not contain curl. Use a tool already present or build it into the image.
  • Wrong address: localhost inside the client is the client, not the API. Use the service name for cross-container requests.
  • Wrong success signal: an HTTP response from a login or maintenance page may not mean the application is ready. Use an endpoint with a defined readiness contract.
  • Persistent DNS error: a healthcheck does not repair mismatched service names or separate networks. See the Docker upstream DNS troubleshooting guide.

5. Replace the demo probe with application readiness

A directory listing only proves that this demo server can respond. For an application, use a lightweight readiness endpoint that returns success only when the dependencies needed to serve requests are usable. Avoid costly model inference in a frequent probe unless you have measured and accepted that cost.

The startup gate is not a substitute for runtime error handling. A dependency can fail after the consumer starts; the application still needs appropriate connection retries and recovery. Do not assume that adding a healthcheck creates an automatic repair system.

When finished, remove only this demo stack from its own directory:

docker compose down

For related deployment guides, use the self-hosting tutorials index.

Sources

Leave a Reply