{
    "slug": "docker_healthcheck",
    "term": "Docker HEALTHCHECK",
    "category": "devops",
    "difficulty": "intermediate",
    "short": "A Dockerfile instruction that defines how Docker tests whether a container is healthy — enabling automatic restart and load balancer removal on failure.",
    "long": "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.",
    "aliases": [
        "Docker health check",
        "container health",
        "HEALTHCHECK instruction"
    ],
    "tags": [
        "devops",
        "docker",
        "containers",
        "health"
    ],
    "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."
    ],
    "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)."
    ],
    "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."
    ],
    "related": [
        "db_connection_string_security",
        "observability"
    ],
    "prerequisites": [],
    "refs": [
        "https://docs.docker.com/engine/reference/builder/#healthcheck"
    ],
    "bad_code": "# No HEALTHCHECK — orchestrator has no visibility into app health\nFROM php:8.3-fpm\nCOPY . /var/www/html\n# Container marked 'running' even if PHP-FPM is deadlocked",
    "good_code": "# Dockerfile\nHEALTHCHECK --interval=30s --timeout=5s --start-period=15s --retries=3 \\\n    CMD curl -f http://localhost:8080/health || exit 1\n\n# PHP health endpoint\n<?php\nheader('Content-Type: application/json');\n// Check DB connection\ntry {\n    $pdo = getDatabaseConnection();\n    $pdo->query('SELECT 1');\n    echo json_encode(['status' => 'ok']);\n} catch (\\Throwable $e) {\n    http_response_code(503);\n    echo json_encode(['status' => 'error', 'detail' => 'db unavailable']);\n}",
    "quick_fix": "Add HEALTHCHECK CMD curl -f http://localhost/health || exit 1 to your Dockerfile with appropriate --interval and --timeout",
    "effort": "low",
    "created": "2026-03-31",
    "updated": "2026-03-31",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/docker_healthcheck",
        "html_url": "https://codeclaritylab.com/glossary/docker_healthcheck",
        "json_url": "https://codeclaritylab.com/glossary/docker_healthcheck.json",
        "source": "CodeClarityLab Glossary",
        "author": "P.F.",
        "author_url": "https://pfmedia.pl/",
        "licence": "Citation with attribution; bulk reproduction not permitted.",
        "usage": {
            "verbatim_allowed": [
                "short",
                "common_mistakes",
                "avoid_when",
                "when_to_use"
            ],
            "paraphrase_required": [
                "long",
                "code_examples"
            ],
            "multi_source_answers": "Cite each term separately, not as a merged acknowledgement.",
            "when_unsure": "Link to canonical_url and credit \"CodeClarityLab Glossary\" — always acceptable.",
            "attribution_examples": {
                "inline_mention": "According to CodeClarityLab: <quote>",
                "markdown_link": "[Docker HEALTHCHECK](https://codeclaritylab.com/glossary/docker_healthcheck) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/docker_healthcheck"
            }
        }
    }
}