Container health

2 snippets in Docker

DKDocker

HEALTHCHECK - Container Health Probe

DK · Dockerfile
Syntax
HEALTHCHECK [options] CMD <command>
HEALTHCHECK NONE
Example
HEALTHCHECK --interval=30s --timeout=5s --retries=3 \
  CMD curl -f http://localhost:3000/health || exit 1

Note Docker marks the container as healthy, unhealthy, or starting based on exit codes. Orchestrators use this to decide when to route traffic or restart containers. Install curl or wget in your image if you use HTTP checks.

Add Health Checks

DK · Best Practices
Syntax
HEALTHCHECK --interval=30s --timeout=5s --retries=3 CMD <check>
Example
HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \
  CMD wget --no-verbose --tries=1 --spider http://localhost:3000/health || exit 1

Note Use wget instead of curl in Alpine images (wget is built in, curl is not). The --start-period gives slow-starting apps time to initialize before health checks count against them.

Frequently asked questions

How does Docker handle container health?
Docker covers this with 2 copy-ready snippets on this page. The "HEALTHCHECK - Container Health Probe" snippet in Docker uses `HEALTHCHECK [options] CMD <command>`.
Which command does the Docker example use?
The "HEALTHCHECK - Container Health Probe" snippet uses `HEALTHCHECK [options] CMD <command>`, from the Dockerfile section of the Docker cheat sheet.
What other Docker snippets are shown for "container health"?
Besides "HEALTHCHECK - Container Health Probe", this page also shows "Add Health Checks".
Is there anything to watch out for?
Yes. For "HEALTHCHECK - Container Health Probe": Docker marks the container as healthy, unhealthy, or starting based on exit codes. Orchestrators use this to decide when to route traffic or restart containers. Install curl or wget in your image if you use HTTP checks.