By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
Docker healthchecks allow you to define a command to run to check the health of a container. HEALTHCHECK command in Dockerfile.
echo "HEALTHCHECK --interval=10s --timeout=3s --retries=3 /bin/bash -c \"curl -s http://localhost:8080 || exit 1\"" >> Dockerfile
docker build -t myimage .
docker run -d --name mycontainer myimage
docker ps -a --format "{{.ID}} {{.Status}}"
Output: mycontainer unhealthy5. Delete the container:
mycontainer unhealthy
docker rm -f mycontainer
docker rmi myimage
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.
docker run -d --name mycontainer -p 8080:8080 -e HEALTHCHECK_CMD="curl -s http://localhost:8080 || exit 1" myimage
-e HEALTHCHECK_CMD
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.
docker run -d --name mycontainer -p 5432:5432 -e HEALTHCHECK_CMD="nc -z localhost 5432 || exit 1" myimage
nc
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.
docker run -d --name mycontainer -e HEALTHCHECK_CMD="/bin/bash -c \"[ -x /app/mybinary ] || exit 1\"" myimage
/bin/bash -c
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.
docker: Error response from daemon: HEALTHCHECK command not found in Dockerfile
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.
docker: Error response from daemon: HEALTHCHECK command failed
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.
docker: Error response from daemon: HEALTHCHECK interval too short
Situation: Container is not responding. Command: docker ps -a --format "{{.ID}} {{.Status}}" Correct response: mycontainer unhealthy
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
docker exec -it mycontainer /bin/bash -c "ps -ef | grep HEALTHCHECK"
grep: HEALTHCHECK: No such file or directory
Situation: HEALTHCHECK interval is set too long. Command: docker inspect mycontainer --format "{{.State.Health.Interval}}" Correct response: 10s
docker inspect mycontainer --format "{{.State.Health.Interval}}"
10s
• 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.
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.