← CodeClarityLab Home
Browse by Category
+ added · updated 7d
← Back to glossary

Docker HEALTHCHECK

devops Intermediate

Also Known As

Docker health check container health HEALTHCHECK instruction

TL;DR

A Dockerfile instruction that defines how Docker tests whether a container is healthy — enabling automatic restart and load balancer removal on failure.

Explanation

HEALTHCHECK CMD defines a command run periodically inside the container. Docker tracks three states: starting (grace period), healthy, unhealthy. After a configurable number of consecutive failures (--retries), the container is marked unhealthy. Docker Swarm and Kubernetes use this signal to restart unhealthy containers and remove them from load balancer rotation. Without HEALTHCHECK, a container that is running but serving errors is invisible to the orchestrator.

Common Misconception

If the container process is running, the container is healthy. A process can be alive but serving errors, deadlocked, or unable to reach its dependencies — HEALTHCHECK detects this.

Why It Matters

A process can be running but completely broken — responding with 500 errors, deadlocked, or unable to connect to the database. HEALTHCHECK makes this observable to the orchestrator so it can act automatically.

Common Mistakes

  • Setting --interval too short on slow-starting apps — container is marked unhealthy before it has finished initialising.
  • Not having a dedicated /health endpoint — using the app's main route mixes health checks with request logs.
  • Forgetting that curl must be installed in the container image for curl-based health checks.

Avoid When

  • Do not use the main application route as the health check endpoint — it pollutes access logs and may have side effects.
  • Do not set --start-period too short for slow-starting applications — they will be killed before they finish initialising.

When To Use

  • Add HEALTHCHECK to every production Docker image — it is the orchestrator's only window into application health.
  • Use a dedicated /health endpoint that checks all critical dependencies (DB, cache, queue).

Code Examples

✗ Vulnerable
# No HEALTHCHECK — orchestrator has no visibility into app health
FROM php:8.3-fpm
COPY . /var/www/html
# Container marked 'running' even if PHP-FPM is deadlocked
✓ Fixed
# Dockerfile
HEALTHCHECK --interval=30s --timeout=5s --start-period=15s --retries=3 \
    CMD curl -f http://localhost:8080/health || exit 1

# PHP health endpoint
<?php
header('Content-Type: application/json');
// Check DB connection
try {
    $pdo = getDatabaseConnection();
    $pdo->query('SELECT 1');
    echo json_encode(['status' => 'ok']);
} catch (\Throwable $e) {
    http_response_code(503);
    echo json_encode(['status' => 'error', 'detail' => 'db unavailable']);
}

Added 31 Mar 2026
Views 19
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings W 0 pings T 0 pings F 0 pings S 0 pings S 0 pings M 3 pings T 0 pings W 0 pings T 0 pings F 0 pings S 1 ping S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 0 pings S 0 pings S 0 pings M 1 ping T 0 pings W 1 ping T 1 ping F 0 pings S 1 ping S 1 ping M 1 ping T 0 pings W 0 pings T
No pings yet today
No pings yesterday
Google 7 Unknown AI 3 SEMrush 3 Perplexity 1 Ahrefs 1
crawler 12 crawler_json 3
DEV INTEL Tools & Severity
⚙ Fix effort: Low
⚡ Quick Fix
Add HEALTHCHECK CMD curl -f http://localhost/health || exit 1 to your Dockerfile with appropriate --interval and --timeout
📦 Applies To
web cli
🔍 Detection Hints
Dockerfile without HEALTHCHECK instruction
Auto-detectable: ✓ Yes hadolint
⚠ Related Problems
🤖 AI Agent
Confidence: High False Positives: Low ✗ Manual fix Fix: Low Context: File

✓ schema.org compliant