| |

Docker 36 ๐Ÿณ Container Healthchecks: HEALTHCHECK Instruction, Exit Codes, and Auto-healing Policies

โฑ๏ธ Estimated read: 17 min | |

A running container is not necessarily a working container. The main process can be alive while the application inside it is wedged โ€” a web server stuck in an infinite loop, a database accepting connections but unable to serve queries, a worker that has lost its connection to the queue. Docker’s HEALTHCHECK instruction exists to distinguish between “the process is running” and “the service is working.” It defines a command that Docker runs periodically inside the container, and based on the command’s exit code, the container receives a health status: starting, healthy, or unhealthy.

The instruction is only half the story. A health status on its own does nothing. Docker’s built-in restart policies โ€” no, on-failure, always, unless-stopped โ€” react to the container’s main process exiting, not to its health status. A container that is unhealthy but still running will not be restarted by restart: unless-stopped. This separation is deliberate and frequently misunderstood. Auto-healing requires either an external mechanism that watches the health status and acts on it, or orchestration platforms like Docker Swarm and Kubernetes that have native health-triggered restart behavior.

This chapter covers three areas. First, why healthchecks exist โ€” the problem of distinguishing process liveness from service readiness, and why the status alone is not enough. Second, how the HEALTHCHECK instruction works โ€” the command, the timing options, the three exit codes, and the state transitions from starting to healthy or unhealthy. Third, how auto-healing is actually achieved โ€” the gap between restart policies and health status, the depends_on health condition for startup ordering, and external auto-heal sidecars that poll the Docker socket and restart unhealthy containers. The chapter ends with a complete example session, a quick reference, best practices, common pitfalls, real-world examples, and diagrams showing the health state machine and the auto-heal architecture.

Key point: The HEALTHCHECK instruction defines a command whose exit code determines the container’s health status. Exit 0 is healthy, exit 1 is unhealthy, and exit 2 is reserved. Docker’s restart policies do not act on health status โ€” they only react to process exit. Auto-healing an unhealthy-but-running container requires an external sidecar (like autoheal) or an orchestrator with native health-triggered restarts .


Why healthchecks and auto-healing exist

The process-liveness problem. A container’s status is Up when its main process is running. But a process can be running and still be broken. A Node.js server can be alive while its event loop is blocked. A PostgreSQL instance can be alive while recovering from a crash and rejecting all connections. A Java application can be alive while deadlocked. Without a healthcheck, Docker has no way to know that the container is not doing its job. The HEALTHCHECK instruction provides a second signal โ€” not just “is the process running,” but “is the service responding” .

The restart-policy gap. Docker’s restart policies are one of the most misunderstood features in the ecosystem. A common assumption is that restart: unless-stopped will restart a container that becomes unhealthy. This is false. Restart policies react to the container’s main process exiting. An unhealthy container that is still running โ€” the process is alive, it just is not working โ€” is never restarted by any built-in policy. The container sits in the unhealthy state indefinitely, and the restart policy never fires . This gap is the reason auto-heal sidecars exist.

The startup-ordering problem. Docker Compose’s basic depends_on only waits for a container to be started, not for it to be ready. A web service that depends on a database will start as soon as the database container is running, even if PostgreSQL is still initializing and not yet accepting connections. The condition: service_healthy option solves this by making the dependency wait for the healthcheck to pass. This is the most common and most legitimate use of the HEALTHCHECK instruction: not for auto-healing, but for correct startup ordering .

The auto-healing problem. If restart policies do not act on health, and the container is wedged but running, how do you recover? The answer is external. A sidecar container โ€” commonly willfarrell/autoheal โ€” connects to the Docker socket, listens for health status events, and issues a restart command when a container becomes unhealthy. This adds a privileged Docker-socket mount to the deployment, which has security implications, but it is the standard pattern for self-healing in plain Docker and Compose . Kubernetes and Docker Swarm have this behavior built in: an unhealthy task is rescheduled, and a failing liveness probe restarts the container .

The false-comfort problem. A healthcheck that passes can create a false sense of security if it only checks that the process is listening, not that the service is functional. A /health endpoint that returns 200 as long as the HTTP server is up โ€” but does not check the database, the queue, or the file system โ€” will report healthy even when the application cannot serve real requests. The healthcheck must verify the things that matter. The DISA specification and Game Warden compliance require healthchecks for exactly this reason: to validate that the container service is still executing and handling workloads .

The trade-off. Healthchecks add overhead. Every check runs a command inside the container, and a poorly designed check can consume CPU, open unnecessary connections, or interfere with the application. The timing parameters โ€” interval, timeout, retries, start_period โ€” must be tuned for the application’s actual startup and response times. Too aggressive, and the container flaps between starting and unhealthy during legitimate slow boots. Too lenient, and a genuinely broken container stays healthy for too long. The auto-heal sidecar adds a security cost (Docker socket access) and a reliability risk (a restart loop can be self-sustaining if the healthcheck itself is the problem) .


a. The HEALTHCHECK instruction

The HEALTHCHECK instruction is placed in a Dockerfile and defines a command that Docker runs periodically inside the container. The command’s exit status determines the container’s health .

HEALTHCHECK --interval=30s --timeout=10s --retries=3 --start-period=60s \
  CMD curl -f http://localhost:8080/health || exit 1

The command can be a shell command (CMD ...) or an exec array (CMD ["...", "..."]). The options before CMD control the timing:

  • --interval=DURATION (default 30s): How long to wait between checks. The first check runs after the interval elapses from container start, and subsequent checks run after each previous check completes.
  • --timeout=DURATION (default 30s): How long a single check is allowed to run before it is considered a failure.
  • --retries=N (default 3): How many consecutive failures are required before the container is marked unhealthy.
  • --start-period=DURATION (default 0s): A grace period during which probe failures do not count toward the retry limit. This is for applications that need time to bootstrap. If a check succeeds during this period, the container is considered started, and subsequent failures count normally.
  • --start-interval=DURATION (default 5s, Docker Engine 25.0+): The interval between checks during the start period.

There can only be one HEALTHCHECK instruction in a Dockerfile. If multiple are listed, only the last one takes effect .

b. Health status and exit codes

A container with a healthcheck has a health status in addition to its normal status. The status starts as starting. When a check passes, the status becomes healthy. After retries consecutive failures, the status becomes unhealthy. Once healthy, a single failure does not change the status โ€” only retries consecutive failures do. Once unhealthy, a single success returns the status to healthy .

The exit code of the healthcheck command determines the result :

  • 0: Success โ€” the container is healthy and ready for use.
  • 1: Failure โ€” the container is not working correctly.
  • 2: Reserved โ€” do not use this exit code. Its behavior is undefined.

Output written to stdout or stderr by the healthcheck command is stored in the health status and can be queried with docker inspect. Only the first 4096 bytes are retained. When the health status changes, a health_status event is generated .

c. Auto-healing with sidecars and orchestration

Because Docker’s restart policies do not act on health status, auto-healing an unhealthy-but-running container requires an external mechanism. The standard pattern for plain Docker and Compose is the autoheal sidecar.

The sidecar connects to the Docker socket, listens for health status events, and issues a restart for containers labeled with autoheal=true. A typical Compose configuration looks like this :

services:
  autoheal:
    image: willfarrell/autoheal:1.2.0
    restart: always
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
    environment:
      - AUTOHEAL_INTERVAL=15
      - AUTOHEAL_START_PERIOD=300
      - AUTOHEAL_DEFAULT_STOP_TIMEOUT=30

  postgres:
    image: postgres:16
    labels:
      - autoheal=true
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres"]
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 60s

The AUTOHEAL_START_PERIOD is a grace period for the autoheal container’s own startup, not a per-target grace period. It protects the initial docker compose up and host reboots, where everything starts together. It does not protect individual containers later .

The autoheal pattern has a significant risk: a self-sustaining restart loop. If the healthcheck fails because the container is genuinely still booting โ€” for example, PostgreSQL replaying WAL after an unclean shutdown โ€” and start_period is not set, the container can be marked unhealthy and restarted before it has a chance to become healthy. The restart is unclean (SIGKILL after the stop timeout), which causes more WAL replay on the next boot, which fails the healthcheck again, which triggers another restart. The loop is self-sustaining and can prevent the database from ever becoming healthy .

For Docker Swarm and Kubernetes, the behavior is different. Swarm reschedules unhealthy tasks. Kubernetes ignores the Dockerfile HEALTHCHECK and uses its own livenessProbe and readinessProbe. A failing liveness probe causes the container to be restarted; a failing readiness probe removes it from service endpoints without restarting it .


Complete Example Session

# ============================================
# PART 1: A BASIC HEALTHCHECK
# ============================================

FROM node:20-alpine

WORKDIR /app
COPY package*.json ./
RUN npm ci --production
COPY . .

# The healthcheck runs every 30 seconds.
# It has 10 seconds to complete.
# It takes 3 consecutive failures to mark the container unhealthy.
# It gives the app 60 seconds to start before failures count.
HEALTHCHECK --interval=30s --timeout=10s --retries=3 --start-period=60s \
  CMD wget --spider -q http://localhost:3000/health || exit 1

EXPOSE 3000
CMD ["node", "server.js"]


# ============================================
# PART 2: HEALTH STATUS IN DOCKER PS
# ============================================

# $ docker ps
# CONTAINER ID   STATUS
# abc123         Up 2 minutes (healthy)
# def456         Up 30 seconds (health: starting)
# ghi789         Up 5 minutes (unhealthy)


# ============================================
# PART 3: INSPECTING HEALTH LOGS
# ============================================

# $ docker inspect --format '{{json .State.Health}}' abc123 | jq
# {
#   "Status": "healthy",
#   "FailingStreak": 0,
#   "Log": [
#     {
#       "Start": "2025-01-01T00:00:00Z",
#       "End": "2025-01-01T00:00:01Z",
#       "ExitCode": 0,
#       "Output": ""
#     }
#   ]
# }


# ============================================
# PART 4: THE RESTART POLICY GAP
# ============================================

# A container with restart: unless-stopped that becomes
# unhealthy will NOT be restarted. The restart policy
# only acts when the main process exits.

# $ docker run -d --restart unless-stopped myapp
# (container becomes unhealthy but keeps running)
# (no restart occurs)


# ============================================
# PART 5: AUTO-HEAL SIDECAR IN COMPOSE
# ============================================

# docker-compose.yml
services:
  app:
    image: myapp
    labels:
      - autoheal=true
    restart: unless-stopped
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:3000/health"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 60s

  autoheal:
    image: willfarrell/autoheal:1.2.0
    restart: always
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
    environment:
      - AUTOHEAL_INTERVAL=15
      - AUTOHEAL_START_PERIOD=300
      - AUTOHEAL_DEFAULT_STOP_TIMEOUT=30

# When the app container becomes unhealthy, autoheal
# detects the health_status event and issues a restart.


# ============================================
# PART 6: STARTUP ORDERING WITH HEALTHCHECK
# ============================================

services:
  db:
    image: postgres:16
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres"]
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 60s

  api:
    image: myapi
    depends_on:
      db:
        condition: service_healthy
    # The api container starts only after the db
    # healthcheck passes, not just after the db
    # container is running.


# ============================================
# PART 7: THE SELF-SUSTAINING RESTART LOOP
# ============================================

# If a container has no start_period and is slow to boot,
# the healthcheck can fail during boot, autoheal restarts it,
# the restart is unclean, boot takes longer, the healthcheck
# fails again, and the loop is self-sustaining.

# FIX: always set start_period to the realistic boot time.
healthcheck:
  test: ["CMD-SHELL", "pg_isready -U postgres"]
  interval: 10s
  timeout: 5s
  retries: 5
  start_period: 180s  # 3 minutes for WAL replay


# ============================================
# PART 8: KUBERNETES VS DOCKER HEALTHCHECK
# ============================================

# Docker Compose uses the Dockerfile HEALTHCHECK.
# Kubernetes ignores it and uses its own probes.

# Kubernetes pod spec:
# livenessProbe:
#   httpGet:
#     path: /health
#     port: 3000
#   initialDelaySeconds: 60
#   periodSeconds: 30
#   failureThreshold: 3

# A failing liveness probe restarts the container.
# A failing readiness probe removes it from service.


# ============================================
# PART 9: HEALTHCHECK EXIT CODES
# ============================================

# Exit 0: healthy
# Exit 1: unhealthy
# Exit 2: reserved, do not use

# The command must exit with the correct code.
# A shell command like `curl -f ... || exit 1`
# ensures exit 1 on failure.


# ============================================
# PART 10: THE COMPLETE PATTERN
# ============================================

# Dockerfile
FROM python:3.12-slim

WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .

HEALTHCHECK --interval=30s --timeout=10s --retries=3 --start-period=30s \
  CMD python -c "import urllib.request; urllib.request.urlopen('http://localhost:8000/health')" || exit 1

EXPOSE 8000
CMD ["python", "app.py"]

# docker-compose.yml
services:
  api:
    build: .
    restart: unless-stopped
    labels:
      - autoheal=true

  autoheal:
    image: willfarrell/autoheal:1.2.0
    restart: always
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
    environment:
      - AUTOHEAL_INTERVAL=15
      - AUTOHEAL_START_PERIOD=300

The ten parts show a basic healthcheck, the status in docker ps, inspecting health logs, the restart-policy gap, an autoheal sidecar, startup ordering with condition: service_healthy, the self-sustaining restart loop, Kubernetes vs Docker, exit codes, and a complete pattern.


Quick Reference

HEALTHCHECK Options

OptionDefaultMeaning
--interval30sTime between checks
--timeout30sMax time for one check
--retries3Consecutive failures to mark unhealthy
--start-period0sGrace period; failures do not count
--start-interval5sInterval during start period (Engine 25.0+)

Exit Codes

CodeMeaning
0Healthy
1Unhealthy
2Reserved โ€” do not use

Health States

StateTransition
startingInitial state; failures during start-period do not count
healthyCheck passed
unhealthyretries consecutive failures

Auto-healing Options

MechanismActs on health?Environment
Restart policiesNoDocker, Compose
autoheal sidecarYesDocker, Compose
Swarm reschedulingYesDocker Swarm
Kubernetes liveness probeYesKubernetes

Compose depends_on Conditions

ConditionMeaning
service_startedContainer is running (default)
service_healthyHealthcheck passes
service_completed_successfullyExits with code 0

Best Practices

โœ… Do This:

# Set a realistic start-period for slow-booting apps
HEALTHCHECK --start-period=60s CMD curl -f http://localhost/health || exit 1   # โœ…
# Use lightweight tools already in the image
HEALTHCHECK CMD wget --spider -q http://localhost/health || exit 1             # โœ…
# Set autoheal=true labels on containers to be healed
labels: [autoheal=true]                                                        # โœ…
# Use condition: service_healthy for dependency ordering
depends_on: { db: { condition: service_healthy } }                             # โœ…
# Keep health checks lightweight
HEALTHCHECK CMD pg_isready -U postgres                                         # โœ…

โŒ Don’t Do This:

# Don't expect restart: unless-stopped to heal unhealthy containers
restart: unless-stopped  # does NOT act on health                            # โŒ
# Don't omit start-period for databases with recovery time
HEALTHCHECK CMD pg_isready  # no start-period, restart loop risk             # โŒ
# Don't use exit code 2
HEALTHCHECK CMD check || exit 2                                              # โŒ
# Don't run heavy queries in a health check
HEALTHCHECK CMD curl /expensive-report                                       # โŒ
# Don't mount the Docker socket without understanding the security cost
volumes: [/var/run/docker.sock:/var/run/docker.sock]                         # โš ๏ธ

Common Pitfalls

PitfallWhy It HappensFix
Container unhealthy but not restartedRestart policy ignores healthUse autoheal sidecar or orchestrator
Self-sustaining restart loopNo start_period on slow-boot containerSet realistic start_period
Health check fails during normal bootstart_period too shortIncrease start_period
False healthy statusCheck only verifies process is listeningCheck critical dependencies
Startup race conditiondepends_on without service_healthyUse condition: service_healthy
Health check overheadHeavy command in health checkUse lightweight check
Exit code 2 usedMisunderstanding reserved codeUse 0 or 1 only

Real-World Examples

1. Basic HTTP Healthcheck

HEALTHCHECK CMD curl -f http://localhost:8080/health || exit 1

2. PostgreSQL Healthcheck

healthcheck:
  test: ["CMD-SHELL", "pg_isready -U postgres"]
  interval: 10s
  retries: 5
  start_period: 60s

3. Redis Healthcheck

healthcheck:
  test: ["CMD", "redis-cli", "ping"]
  interval: 10s
  timeout: 5s
  retries: 3

4. Autoheal Sidecar

autoheal:
  image: willfarrell/autoheal:1.2.0
  volumes: [/var/run/docker.sock:/var/run/docker.sock]

5. Dependency Ordering

depends_on:
  db: { condition: service_healthy }

6. Health Status Inspection

docker inspect --format '{{json .State.Health}}' container

7. Start Period for Slow Boot

HEALTHCHECK --start-period=180s CMD pg_isready || exit 1

8. wget in Alpine

HEALTHCHECK CMD wget --spider -q http://localhost/ || exit 1

9. Autoheal Label

labels: [autoheal=true]

10. Kubernetes Liveness Probe

livenessProbe:
  httpGet: { path: /health, port: 3000 }

Visual

Health State Machine

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  HEALTH STATE MACHINE                                        โ”‚
โ”‚                                                              โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”                                            โ”‚
โ”‚  โ”‚  starting    โ”‚  โ† initial state                           โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜                                            โ”‚
โ”‚         โ”‚                                                    โ”‚
โ”‚         โ”‚  check passes                                      โ”‚
โ”‚         โ–ผ                                                    โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”                                            โ”‚
โ”‚  โ”‚  healthy     โ”‚  โ† check passed                            โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜                                            โ”‚
โ”‚         โ”‚                                                    โ”‚
โ”‚         โ”‚  retries consecutive failures                      โ”‚
โ”‚         โ–ผ                                                    โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”                                            โ”‚
โ”‚  โ”‚  unhealthy   โ”‚  โ† retries consecutive failures            โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜                                            โ”‚
โ”‚         โ”‚                                                    โ”‚
โ”‚         โ”‚  check passes                                      โ”‚
โ”‚         โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–บ back to healthy                     โ”‚
โ”‚                                                              โ”‚
โ”‚  During start-period: failures do not count toward retries.  โ”‚
โ”‚  Once a check passes during start-period, the container is   โ”‚
โ”‚  considered started, and failures count normally.            โ”‚
โ”‚                                                              โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

The Restart Policy Gap

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  RESTART POLICY GAP                                          โ”‚
โ”‚                                                              โ”‚
โ”‚  Container exits with error:                                 โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”‚
โ”‚  โ”‚  restart: unless-stopped  โ†’ RESTARTS                 โ”‚    โ”‚
โ”‚  โ”‚                                                       โ”‚    โ”‚
โ”‚  โ”‚  Main process exits. Policy fires. Container restarts.โ”‚    โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ”‚
โ”‚                                                              โ”‚
โ”‚  Container becomes unhealthy but keeps running:              โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”‚
โ”‚  โ”‚  restart: unless-stopped  โ†’ DOES NOTHING             โ”‚    โ”‚
โ”‚  โ”‚                                                       โ”‚    โ”‚
โ”‚  โ”‚  Main process is alive. Policy does not fire.        โ”‚    โ”‚
โ”‚  โ”‚  Container stays unhealthy indefinitely.             โ”‚    โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ”‚
โ”‚                                                              โ”‚
โ”‚  Auto-healing requires an external actor.                    โ”‚
โ”‚                                                              โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Autoheal Architecture

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  AUTOHEAL SIDECAR                                            โ”‚
โ”‚                                                              โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”                                    โ”‚
โ”‚  โ”‚  app container       โ”‚                                    โ”‚
โ”‚  โ”‚  label: autoheal=trueโ”‚                                    โ”‚
โ”‚  โ”‚  health: unhealthy   โ”‚                                    โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜                                    โ”‚
โ”‚           โ”‚                                                  โ”‚
โ”‚           โ”‚  health_status event                             โ”‚
โ”‚           โ–ผ                                                  โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”                                    โ”‚
โ”‚  โ”‚  autoheal sidecar    โ”‚                                    โ”‚
โ”‚  โ”‚  listens to Docker   โ”‚                                    โ”‚
โ”‚  โ”‚  socket events       โ”‚                                    โ”‚
โ”‚  โ”‚                      โ”‚                                    โ”‚
โ”‚  โ”‚  POST /containers/   โ”‚                                    โ”‚
โ”‚  โ”‚  {id}/restart        โ”‚                                    โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜                                    โ”‚
โ”‚           โ”‚                                                  โ”‚
โ”‚           โ”‚  restart command                                 โ”‚
โ”‚           โ–ผ                                                  โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”                                    โ”‚
โ”‚  โ”‚  app container       โ”‚                                    โ”‚
โ”‚  โ”‚  restarted           โ”‚                                    โ”‚
โ”‚  โ”‚  health: starting    โ”‚                                    โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜                                    โ”‚
โ”‚                                                              โ”‚
โ”‚  The sidecar mounts the Docker socket.                       โ”‚
โ”‚  It acts on health status, which restart policies do not.    โ”‚
โ”‚                                                              โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Dependency Startup with service_healthy

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  DEPENDENCY STARTUP                                          โ”‚
โ”‚                                                              โ”‚
โ”‚  WITHOUT service_healthy:                                    โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”‚
โ”‚  โ”‚  db starts โ†’ api starts immediately                  โ”‚    โ”‚
โ”‚  โ”‚  api tries to connect โ†’ db not ready โ†’ api crashes   โ”‚    โ”‚
โ”‚  โ”‚  api restarts โ†’ db not ready โ†’ api crashes again     โ”‚    โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ”‚
โ”‚                                                              โ”‚
โ”‚  WITH service_healthy:                                       โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”‚
โ”‚  โ”‚  db starts โ†’ healthcheck runs โ†’ passes โ†’ api starts  โ”‚    โ”‚
โ”‚  โ”‚  api connects successfully โ†’ no crash loop           โ”‚    โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ”‚
โ”‚                                                              โ”‚
โ”‚  The healthcheck gates the dependent service's startup.      โ”‚
โ”‚  This is the most common legitimate use of HEALTHCHECK.      โ”‚
โ”‚                                                              โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Summary

ItemValue
HEALTHCHECKInstruction defining a periodic health check command
Exit codes0 = healthy, 1 = unhealthy, 2 = reserved
Health statesstarting, healthy, unhealthy
Restart policiesReact to process exit, not health status
Auto-healingRequires autoheal sidecar or orchestrator
Startup orderingcondition: service_healthy in Compose
Self-sustaining loopRisk when start_period is missing
Security costAutoheal requires Docker socket mount
KubernetesIgnores Dockerfile HEALTHCHECK; uses liveness/readiness probes

Key takeaways:

  • HEALTHCHECK defines a command, not a policy. It determines the container’s health status, but the status alone does not trigger any action. Something else must act on it .
  • Exit codes are the contract. Exit 0 means healthy, exit 1 means unhealthy, and exit 2 is reserved. The command must exit with the correct code, which is why || exit 1 is a common idiom .
  • Restart policies do not read health. restart: always, unless-stopped, and on-failure all react to the main process exiting. An unhealthy container that is still running is never restarted by these policies .
  • Auto-healing requires an external actor. The autoheal sidecar listens for health status events and issues restarts. It mounts the Docker socket, which is a security cost that must be accepted deliberately .
  • start_period prevents restart loops. Without it, a slow-booting container can be marked unhealthy and restarted before it has a chance to become healthy. The restart is unclean, which makes the next boot slower, which fails the healthcheck again. The loop is self-sustaining .
  • condition: service_healthy is the most common use. It makes Docker Compose wait for a dependency’s healthcheck to pass before starting the dependent service. This prevents startup race conditions .
  • Kubernetes ignores Dockerfile HEALTHCHECK. It uses its own livenessProbe and readinessProbe. A failing liveness probe restarts the container; a failing readiness probe removes it from service endpoints .
  • Healthchecks must check the right thing. A check that only verifies the process is listening will report healthy even when the application cannot serve real requests. The check should verify the critical dependencies and operations that define readiness .

Remember: A healthcheck is a signal, not a remedy. It tells Docker whether the container is working, but it does not fix anything on its own. The restart policies that many assume will heal an unhealthy container do not read health status โ€” they only react to process exit. Auto-healing an unhealthy-but-running container requires an external sidecar that watches the Docker socket and restarts it, or an orchestrator with native health-triggered behavior. The most common and most legitimate use of HEALTHCHECK is startup ordering: condition: service_healthy makes a dependent service wait until its dependency is actually ready, not just running. The healthcheck is the foundation, but the auto-healing policy is what acts on it.


Was this helpful?


Stop using slow, ad-bloated tool sites! ๐Ÿคฎ

๐Ÿ”Ž Search “KandZ Tools” on Google to use many professional utilities for free.

KandZ.me is the ultimate minimalist hub for:
โœ… Finance (Mortgage, Interest, Inflation)
โœ… Tech (Base64, JSON, Dev Suite, IP)
โœ… Health (BMI, BMR, TDEE)
โœ… Productivity (Timer, Workspace, QR)

โšก๏ธ Fast & Private
๐Ÿ”’ No data leaves your device
๐Ÿ’Ž 100% Free

๐Ÿ”— Use it now: https://tools.kandz.me
๐Ÿ”– Bookmark itโ€”youโ€™ll need it later!