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.