Software

Docker Compose Health Checks: Make Startup and Recovery More Reliable

A practical guide to container health checks, dependency readiness, restart behavior and troubleshooting unhealthy services.

Sunil Kumar5 min read
Server racks and network infrastructure representing containerized application services

A container can be running while its application is unusable. A process may have started but still be waiting for a database, loading configuration or warming a cache. Docker Compose health checks provide a way to test application readiness instead of relying only on process state.

Choose a meaningful health command

A health check should test the service’s ability to perform its core function. For an HTTP API, a lightweight endpoint can confirm that the application event loop and essential dependencies are ready. For a database, use its native readiness command. Avoid checks that only confirm a port is open if the application can accept a connection but cannot serve requests.

Set intervals and timeouts deliberately

Configure a reasonable interval, timeout, retry count and startup grace period. A slow service may need a longer start period, while a frequently polled expensive endpoint can create unnecessary load. The check should be fast, deterministic and safe to run repeatedly.

Understand dependency ordering

Compose can wait for a dependency marked with a healthy condition before starting a dependent service. This improves initial startup, but it does not replace application-level retry logic. A database can become unavailable after startup, and a service should recover from transient connection failures without requiring the whole stack to be manually restarted.

Do not confuse health with restart policy

A failed health check marks a container unhealthy; it does not automatically guarantee that Compose will restart it. Restart policies generally respond to process exit. Decide whether the application should recover internally, whether an orchestrator should replace it, or whether an operator should investigate. Avoid blind restart loops that hide a persistent configuration problem.

Inspect failures

Use docker compose ps and docker inspect to review health status and recent check output. Check whether the health command exists inside the image, whether it has permission to run and whether it uses the correct address. Remember that localhost inside a container refers to that same container, not another service in the Compose network.

Production checklist: use a cheap readiness endpoint, allow for startup time, retry dependencies in application code, monitor unhealthy status and test what happens when a dependency disappears after startup.

Documentation: Docker Compose service health checks — https://docs.docker.com/compose/how-tos/startup-order/

Distinguish process health from application readiness

A container can remain alive while its service is unable to answer useful requests. A web process may have started but still be loading migrations, warming a cache or waiting for a database connection. A useful health check tests a small operation that reflects readiness without triggering expensive work. Keep liveness and readiness concepts separate in the application design: a process that is alive but temporarily not ready may need time to recover rather than an immediate restart. For Compose deployments, document what an unhealthy status means and who or what is expected to act on it.

Write checks that work inside the image

Minimal container images often omit curl, wget or shell utilities that a health command assumes are present. A health check can therefore fail even though the application is healthy. Prefer a small command or endpoint already supported by the image, and verify the command in the built artifact rather than only on a developer laptop. If the check uses HTTP, target the correct interface and port. Remember that localhost inside a container refers to that container, not a neighboring service. For a database, use the vendor’s native readiness utility where possible.

Set timing based on startup characteristics

Intervals, timeouts, retry counts and startup grace periods should reflect measured behavior. A small API may be ready in seconds, while a search service that loads indexes may need much longer. If the startup grace period is too short, a healthy slow-starting container may be marked unhealthy repeatedly. If checks run too frequently, they can create load during an already stressed incident. Test cold starts after host reboot and image upgrades, not just warm restarts on a developer machine.

Use dependency ordering without assuming permanent availability

Compose can wait for a dependency marked healthy before creating a dependent service, which helps with initial startup sequencing. It does not guarantee that the dependency remains available later. Applications should implement bounded retries, connection timeouts and graceful handling of database or cache interruptions. A web process should not crash in a tight loop because a database is briefly restarting. Conversely, endless retries can consume resources and hide a configuration error. Log the failure clearly and expose a degraded readiness state when the service cannot fulfill its core function.

Understand restart policies and unhealthy states

Health status and process exit are different signals. A container marked unhealthy does not necessarily restart automatically under a standard restart policy because the process may still be running. Decide whether the application can self-heal, whether an external supervisor should replace it or whether an operator needs to inspect the issue. Avoid building a restart loop around every failed probe; repeated restarts can erase useful state and make root-cause analysis harder. Record health transitions and alert when a service remains unhealthy beyond a defined period.

Inspect the actual health-check output

When a container is unhealthy, inspect its recent health log and compare the failure timestamp with application and dependency logs. Check command exit codes, DNS resolution, credentials, permissions and TLS settings. A health check may use an internal endpoint that is blocked by application authentication or host-based access rules. Keep the endpoint intentionally lightweight and avoid returning secrets or detailed internal configuration. If the check fails only under load, measure whether the probe timeout is too aggressive or the service is genuinely saturated.

Test failure and recovery scenarios

Stop a dependency temporarily, introduce a slow response and restart the host to observe the stack. Confirm that dependent services recover without manual intervention and that health status changes back to healthy after the underlying issue is fixed. Test a bad environment variable and a missing migration so the team knows how configuration failures appear. Record expected behavior in the runbook. A reliable container stack should fail visibly, recover predictably and provide enough evidence to distinguish an application defect from an infrastructure problem.

Keep production health checks maintainable

Review health commands whenever the base image, application port or dependency topology changes. Keep the check under version control alongside the Compose file and test it in CI against the built image. Avoid a probe that requires a remote public service; a temporary internet outage should not mark an otherwise healthy local API as broken. Use external synthetic monitoring for end-to-end availability and container health checks for local service readiness. These signals complement each other and should be interpreted together during an incident.

Share this article

Written by

Sunil Kumar

Founder of FutureTechie and BoltPane. Writes clear explainers on AI, cybersecurity, gadgets and developer tools for curious readers who want signal over hype.

106 articles View profile

Get the tech briefing

AI, gadgets, software and cybersecurity — distilled into one short briefing every weekday.

Leave a Reply

Your email address will not be published. Required fields are marked *