📖 Scenario: You are managing a web application running inside a Docker container. To keep the app reliable, you want Docker to check if the app is healthy and restart it if it is not working properly.
🎯 Goal: Learn how to add a health check to a Docker container using a Dockerfile. You will create a simple web server, add a health check command, and see the health status.
📋 What You'll Learn
Create a Dockerfile with a basic web server
Add a health check command using HEALTHCHECK
Use a simple command to test the server health
Build and run the Docker container
Check the container health status
💡 Why This Matters
🌍 Real World
In real projects, health checks help keep applications running smoothly by automatically restarting containers that are not responding.
💼 Career
Knowing how to add and use container health checks is important for DevOps roles to maintain reliable and self-healing systems.
Progress0 / 4 steps
1
Create a basic Dockerfile with a web server
Create a file named Dockerfile with these exact lines to set up a simple web server using nginx image: FROM nginx:alpine
Docker
Hint
This sets the base image to nginx on Alpine Linux, which is a small web server.
2
Add a health check command to the Dockerfile
Add a HEALTHCHECK instruction to the Dockerfile that runs the command curl -f http://localhost/ || exit 1 every 30 seconds with a timeout of 5 seconds. Use HEALTHCHECK --interval=30s --timeout=5s CMD curl -f http://localhost/ || exit 1.
Docker
Hint
The health check runs curl to check if the web server responds successfully.
3
Build and run the Docker container with health check
Build the Docker image with the tag mynginx using docker build -t mynginx .. Then run the container in detached mode with the name webserver using docker run -d --name webserver mynginx.
Docker
Hint
Use the exact commands to build and run the container so Docker can track health.
4
Check the container health status
Use the command docker ps to see the container status. The STATUS column should show Up (healthy) if the health check passes.
Docker
Hint
Look for the word healthy in the status column to confirm the health check works.
Practice
(1/5)
1. What is the main purpose of a HEALTHCHECK in a Docker container?
easy
A. To restart the Docker daemon automatically
B. To update the container's image
C. To check if the container's application is running correctly
D. To increase the container's CPU usage
Solution
Step 1: Understand the role of HEALTHCHECK
The HEALTHCHECK instruction runs a command inside the container to verify if the app is working properly.
Step 2: Identify what HEALTHCHECK does not do
It does not restart Docker daemon, update images, or increase CPU usage.
Final Answer:
To check if the container's application is running correctly -> Option C
Quick Check:
HEALTHCHECK verifies app status = B [OK]
Hint: HEALTHCHECK tests app inside container, not Docker itself [OK]
Common Mistakes:
Confusing HEALTHCHECK with Docker daemon restart
Thinking HEALTHCHECK updates images
Assuming HEALTHCHECK changes resource usage
2. Which of the following is the correct syntax to add a health check in a Dockerfile?
easy
A. HEALTHCHECK CMD curl -f http://localhost/ || exit 1
B. HEALTHCHECK RUN curl -f http://localhost/ || exit 1
C. HEALTHCHECK EXEC curl -f http://localhost/ || exit 1
D. HEALTHCHECK CHECK curl -f http://localhost/ || exit 1
Solution
Step 1: Recall Dockerfile HEALTHCHECK syntax
The correct syntax uses HEALTHCHECK CMD followed by the command to run.
Step 2: Identify incorrect keywords
RUN, EXEC, CHECK are not valid keywords for HEALTHCHECK instruction.
But your container health status is always starting. What is the likely problem?
medium
A. The container has no network access
B. The HEALTHCHECK syntax is invalid
C. Docker daemon is not running
D. The health check command never succeeds within the start period
Solution
Step 1: Understand 'starting' health status meaning
'starting' means the container is still within the initial grace period before health checks are considered final.
Step 2: Analyze why health check stays 'starting'
If the command never succeeds during this period, Docker keeps status as 'starting'. Syntax is correct, Docker daemon running, and network likely accessible.
Final Answer:
The health check command never succeeds within the start period -> Option D
Quick Check:
Failing health check in start period = starting [OK]
Hint: Starting means health check not yet passed in grace period [OK]
Common Mistakes:
Assuming syntax error causes 'starting' status
Blaming Docker daemon when it runs fine
Ignoring network issues but they are less likely here
5. You want to create a health check that retries 3 times before marking unhealthy and waits 10 seconds between checks. Which Dockerfile snippet correctly sets this up?