Fatskills
Practice. Master. Repeat.
Study Guide: Docker: Docker in Production - Healthchecks, HEALTHCHECK in Dockerfile, health flags
Source: https://www.fatskills.com/kubernetes/chapter/docker-docker-docker-in-production-healthchecks-healthcheck-in-dockerfile-health-flags

Docker: Docker in Production - Healthchecks, HEALTHCHECK in Dockerfile, health flags

By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.

⏱️ ~3 min read

1. Core Command / Concept

Docker healthchecks allow you to define a command to run to check the health of a container. HEALTHCHECK command in Dockerfile.

2. Step-by-Step

  1. Create a new Dockerfile with a HEALTHCHECK command:
echo "HEALTHCHECK --interval=10s --timeout=3s --retries=3 /bin/bash -c \"curl -s http://localhost:8080 || exit 1\"" >> Dockerfile
  1. Build the Docker image:
docker build -t myimage .
  1. Run the container:
docker run -d --name mycontainer myimage
  1. Check the container's health:
docker ps -a --format "{{.ID}} {{.Status}}"

Output: mycontainer unhealthy
5. Delete the container:

docker rm -f mycontainer
  1. Clean up:
docker rmi myimage

3. Real Commands with Examples

Example 1: Simple HTTP Check

Purpose: Check if a web server is responding. Command: docker run -d --name mycontainer -p 8080:8080 -e HEALTHCHECK_CMD="curl -s http://localhost:8080 || exit 1" myimage Flags: -e HEALTHCHECK_CMD sets the healthcheck command.

Example 2: TCP Check

Purpose: Check if a database is listening on a port. Command: docker run -d --name mycontainer -p 5432:5432 -e HEALTHCHECK_CMD="nc -z localhost 5432 || exit 1" myimage Flags: -e HEALTHCHECK_CMD sets the healthcheck command, nc is the netcat command.

Example 3: Executable Check

Purpose: Check if a binary is executable. Command: docker run -d --name mycontainer -e HEALTHCHECK_CMD="/bin/bash -c \"[ -x /app/mybinary ] || exit 1\"" myimage Flags: -e HEALTHCHECK_CMD sets the healthcheck command, /bin/bash -c runs the command.

4. Common Errors

Error 1: HEALTHCHECK command not found

Error message: docker: Error response from daemon: HEALTHCHECK command not found in Dockerfile. Why it happens: The HEALTHCHECK command is not defined in the Dockerfile. Fix: Add the HEALTHCHECK command to the Dockerfile.

Error 2: HEALTHCHECK command fails

Error message: docker: Error response from daemon: HEALTHCHECK command failed. Why it happens: The HEALTHCHECK command is failing. Fix: Check the HEALTHCHECK command and fix the issue.

Error 3: HEALTHCHECK interval too short

Error message: docker: Error response from daemon: HEALTHCHECK interval too short. Why it happens: The HEALTHCHECK interval is set too short. Fix: Increase the HEALTHCHECK interval.

5. Quick Checks

Scenario 1: Container is unhealthy

Situation: Container is not responding. Command: docker ps -a --format "{{.ID}} {{.Status}}" Correct response: mycontainer unhealthy

Scenario 2: HEALTHCHECK command not running

Situation: HEALTHCHECK command is not running. Command: docker exec -it mycontainer /bin/bash -c "ps -ef | grep HEALTHCHECK" Correct response: grep: HEALTHCHECK: No such file or directory

Scenario 3: HEALTHCHECK interval too long

Situation: HEALTHCHECK interval is set too long. Command: docker inspect mycontainer --format "{{.State.Health.Interval}}" Correct response: 10s

6. Last-Minute Reference

• HEALTHCHECK command must be defined in the Dockerfile.
• HEALTHCHECK interval must be a positive integer.
• HEALTHCHECK timeout must be a positive integer.
• HEALTHCHECK retries must be a positive integer.
• HEALTHCHECK command must exit with a non-zero status code to indicate failure.
• HEALTHCHECK command must exit with a zero status code to indicate success.
• HEALTHCHECK interval must be at least 1 second.
• HEALTHCHECK timeout must be at least 1 second.
• HEALTHCHECK retries must be at least 1.
• HEALTHCHECK command must not block indefinitely.