Docker HEALTHCHECK

Topic: Containers core

Summary

Add HEALTHCHECK to Dockerfile so Docker reports container health. Use a command that exits 0 when healthy. Use when orchestration or load balancers need health status.

Intent: How-to

Quick answer

  • HEALTHCHECK CMD curl -f http://localhost/health || exit 1. Options: interval, timeout, start_period, retries.
  • docker ps shows (healthy). Orchestrators use this for restarts and routing.
  • Use lightweight check; ensure port or path exists in image.

Prerequisites

Steps

  1. Add HEALTHCHECK

    HEALTHCHECK CMD curl -f http://localhost:8080/health || exit 1. Set interval if needed.

  2. Build and run

    docker build -t app . docker run -d app. docker ps shows (healthy).

  3. Verify

    docker inspect for State.Health.Status. Simulate failure and check.

Summary

Add HEALTHCHECK in Dockerfile; verify with docker ps and inspect.

Prerequisites

Steps

Step 1: Add HEALTHCHECK

HEALTHCHECK CMD with curl or script; exit 1 on failure.

Step 2: Build and run

Build and run; wait for healthy.

Step 3: Verify

Inspect health; test failure path.

Verification

  • docker ps shows (healthy).

Troubleshooting

Unhealthy — Check command and port. Always starting — Increase start_period.

Next steps

Continue to