In this lesson, you will learn how to configure health checks so that Docker Swarm can automatically detect and replace containers that have stopped functioning correctly.
The Concept of Self-Healing
In a distributed system, a container might stay "running" from the perspective of the OS, even if the application inside has deadlocked or crashed. Without an active check, the orchestrator assumes the service is healthy, and traffic continues to be routed to a non-responsive container.
By defining a healthcheck in your service configuration, you instruct Docker Swarm to periodically execute a command inside the container. If that command fails repeatedly, the orchestrator marks the task as unhealthy. Because you are working within a Swarm, the orchestrator will automatically kill the unhealthy instance and spin up a fresh replica to maintain your desired state.
Implementing Health Checks
You define these checks directly in your docker-compose.yml file. Here is how you configure a standard HTTP-based health check:
services:
web-app:
image: my-app:latest
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:8080/health"]
interval: 30s
timeout: 10s
retries: 3
start_period: 40s
test: The actual command to run. Usingcurl -fensures that any non-zero exit code (like a 404 or 500 error) is treated as a failure.interval: How often to run the check.timeout: How long to wait for the command to succeed before counting it as a failure.retries: How many consecutive failures are allowed before the container is markedunhealthy.start_period: A buffer that allows your application time to boot up before the health checks begin, preventing premature restarts during initialization.
Once this is defined, if the curl command fails three times in a row, Swarm detects the unhealthy status and replaces the container immediately. This creates a "self-healing" loop where your service maintains high availability without human intervention.
Key Takeaway: Defining a healthcheck turns your service into a self-healing unit that monitors its own internal state and triggers automatic recovery by the Swarm orchestrator when failures occur.