📖 Scenario: You are managing a Docker container running a simple web application. You want to make sure the container is healthy and get alerted if it becomes unhealthy. This helps keep your app running smoothly.
🎯 Goal: Set up a Docker container with a health check and create a simple alert command to notify if the container is unhealthy.
📋 What You'll Learn
Create a Docker container running the nginx image
Add a health check command to the container configuration
Write a command to check the container's health status
Print an alert message if the container is unhealthy
💡 Why This Matters
🌍 Real World
Monitoring container health is important to keep applications running smoothly and avoid downtime.
💼 Career
DevOps engineers often set up health checks and alerts to maintain reliable containerized applications.
Progress0 / 4 steps
1
Create a Docker container running nginx
Write the docker run command to start a container named mynginx using the nginx image in detached mode.
Docker
Hint
Use docker run -d --name mynginx nginx to start the container in the background.
2
Add a health check to the container
Write a docker run command to start a container named mynginx with a health check that runs curl -f http://localhost/ || exit 1 every 10 seconds, with a timeout of 5 seconds and retries set to 3.
Docker
Hint
Use --health-cmd, --health-interval, --health-timeout, and --health-retries options with docker run.
3
Check the container's health status
Write a docker inspect command to get the health status of the container named mynginx. Use jq to extract the health status from the JSON output.
Docker
Hint
Use docker inspect --format='{{json .State.Health.Status}}' mynginx and save it to a variable.
4
Print an alert if the container is unhealthy
Write a bash if statement that checks if the variable health_status equals unhealthy. If yes, print "Alert: Container mynginx is unhealthy!".
Docker
Hint
Use if [ "$health_status" = "unhealthy" ] and echo to print the alert.
Practice
(1/5)
1. What is the main purpose of setting up a health check for a Docker container?
easy
A. To automatically monitor if the container is working properly
B. To increase the container's CPU usage
C. To change the container's network settings
D. To delete the container after a fixed time
Solution
Step 1: Understand health check purpose
Health checks are used to monitor if a container is running as expected.
Step 2: Identify correct purpose
Among the options, only monitoring container health matches the purpose.
Final Answer:
To automatically monitor if the container is working properly -> Option A
Quick Check:
Health check purpose = monitor container health [OK]
Hint: Health checks watch container status automatically [OK]
Common Mistakes:
Thinking health checks change container settings
Confusing health checks with resource limits
Assuming health checks delete containers
2. Which of the following is the correct syntax to add a health check command 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 TEST curl -f http://localhost/ || exit 1
Solution
Step 1: Recall Dockerfile health check syntax
The correct Dockerfile instruction is HEALTHCHECK followed by CMD and the command.
Step 2: Match syntax with options
Only HEALTHCHECK CMD curl -f http://localhost/ || exit 1 uses HEALTHCHECK CMD which is valid syntax.
A. The container health status will be 'healthy' regardless of failures
B. The container health status will be 'unhealthy' after failures
C. The container will stop immediately on first failure
D. The container will restart automatically on failure
Solution
Step 1: Understand health check failure behavior
If the health check command fails repeatedly, Docker marks the container as 'unhealthy'.
Step 2: Analyze options
Only The container health status will be 'unhealthy' after failures correctly states the container becomes 'unhealthy' after failures.
Final Answer:
The container health status will be 'unhealthy' after failures -> Option B
Quick Check:
Failed health checks = 'unhealthy' status [OK]
Hint: Failed health checks mark container as unhealthy [OK]
Common Mistakes:
Thinking container stops immediately on failure
Assuming container restarts automatically on health failure
Believing health status stays 'healthy' despite failures
4. You wrote this Dockerfile snippet but the health check is not working as expected:
HEALTHCHECK CMD curl http://localhost/ || exit 1
What is the likely problem?
medium
A. Health check commands cannot use curl
B. Using CMD instead of RUN in HEALTHCHECK
C. The exit code should be 0 on failure
D. Missing the '-f' flag in curl to fail on HTTP errors
Solution
Step 1: Understand curl behavior in health checks
Without '-f', curl returns success even on HTTP errors, so health check may falsely pass.
Step 2: Identify correct fix
Adding '-f' makes curl fail on HTTP errors, correctly marking container unhealthy.
Final Answer:
Missing the '-f' flag in curl to fail on HTTP errors -> Option D
Quick Check:
Use curl -f to detect HTTP failures in health checks [OK]
Hint: Add '-f' to curl for proper failure detection [OK]
Common Mistakes:
Confusing CMD with RUN in HEALTHCHECK
Thinking curl cannot be used in health checks
Using exit 0 to indicate failure
5. You want to set up an alert system that triggers when a Docker container becomes unhealthy. Which approach correctly combines Docker health checks with alerting?
hard
A. Set the container to restart automatically without health checks
B. Only rely on Docker logs to detect container issues, no health checks needed
C. Use Docker HEALTHCHECK in the Dockerfile and monitor container health status with an external tool like Prometheus
D. Manually run health check commands inside the container every hour
Solution
Step 1: Combine health checks with monitoring tools
Docker HEALTHCHECK defines how to test container health automatically.
Step 2: Use external monitoring for alerts
Tools like Prometheus can watch Docker health status and trigger alerts when unhealthy.
Step 3: Evaluate other options
Options B, C, and D miss automatic health monitoring or alerting integration.
Final Answer:
Use Docker HEALTHCHECK in the Dockerfile and monitor container health status with an external tool like Prometheus -> Option C
Quick Check:
Healthcheck + monitoring tool = alert setup [OK]
Hint: Combine HEALTHCHECK with monitoring tools for alerts [OK]
Common Mistakes:
Ignoring health checks and relying only on logs
Assuming restart policies replace health monitoring
Doing manual checks instead of automated monitoring
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.