Health checks in Docker in Spring Boot - Step-by-Step Execution
HEALTHCHECK --interval=10s --timeout=5s \ CMD curl -f http://localhost:8080/actuator/health || exit 1
| Step | Action | Command Output | Docker Health Status | Container State |
|---|---|---|---|---|
| 1 | Docker starts container | N/A | starting | container running |
| 2 | Docker runs health check command | HTTP 200 {"status":"UP"} | healthy | container running |
| 3 | Wait 10 seconds | N/A | healthy | container running |
| 4 | Docker runs health check command | HTTP 200 {"status":"UP"} | healthy | container running |
| 5 | Wait 10 seconds | N/A | healthy | container running |
| 6 | Docker runs health check command | curl: (7) Failed to connect | unhealthy | container running |
| 7 | Docker marks container unhealthy | N/A | unhealthy | container running |
| 8 | Docker restarts container | N/A | starting | container restarting |
| 9 | Docker runs health check command | HTTP 200 {"status":"UP"} | healthy | container running |
| Variable | Start | After 2 | After 4 | After 6 | After 8 | After 9 |
|---|---|---|---|---|---|---|
| Docker Health Status | starting | healthy | healthy | unhealthy | starting | healthy |
| Container State | container running | container running | container running | container running | container restarting | container running |
Docker health checks run a command periodically. Spring Boot actuator /health endpoint returns app status. Docker marks container healthy if command succeeds. If command fails, Docker marks unhealthy and can restart. Use HEALTHCHECK in Dockerfile with curl to actuator. This helps keep apps running smoothly.
Practice
What is the main purpose of adding a HEALTHCHECK instruction in a Dockerfile for a Spring Boot application?
Solution
Step 1: Understand the role of HEALTHCHECK in Docker
The HEALTHCHECK instruction tells Docker how to test if the container's app is healthy.Step 2: Connect HEALTHCHECK to app monitoring
It helps Docker detect problems and restart the app if it stops working properly.Final Answer:
To let Docker monitor if the app is running correctly and restart if needed -> Option AQuick Check:
HEALTHCHECK monitors app health [OK]
- Thinking HEALTHCHECK speeds up app startup
- Confusing HEALTHCHECK with image size optimization
- Assuming HEALTHCHECK updates app versions automatically
Which of the following is the correct syntax to add a health check in a Dockerfile for a Spring Boot app that exposes /actuator/health on port 8080?
?
Solution
Step 1: Identify correct HEALTHCHECK syntax
The HEALTHCHECK instruction uses CMD followed by a command that returns 0 on success or non-zero on failure.Step 2: Verify the command tests the health endpoint properly
Using curl with --fail returns error if the endpoint is unhealthy, triggering Docker restart.Final Answer:
HEALTHCHECK CMD curl --fail http://localhost:8080/actuator/health || exit 1 -> Option BQuick Check:
HEALTHCHECK CMD + curl --fail = correct syntax [OK]
- Using RUN instead of CMD in HEALTHCHECK
- Using ping instead of curl for HTTP check
- Using EXEC keyword which is invalid
Given this Dockerfile snippet, what will Docker report as the container health status if the Spring Boot app's /actuator/health endpoint returns HTTP 500?
HEALTHCHECK CMD curl --fail http://localhost:8080/actuator/health || exit 1
Solution
Step 1: Understand curl --fail behavior on HTTP 500
The curl --fail option causes curl to exit with error on HTTP status codes 400 or above.Step 2: Connect curl failure to Docker health status
Since curl fails, the HEALTHCHECK command exits with 1, marking container as unhealthy.Final Answer:
unhealthy -> Option DQuick Check:
HTTP 500 triggers curl failure = unhealthy [OK]
- Assuming HTTP 500 still means healthy
- Confusing 'starting' with unhealthy status
- Thinking Docker ignores health check failures
Identify the error in this Dockerfile health check instruction for a Spring Boot app:
HEALTHCHECK CMD curl http://localhost:8080/actuator/health || exit 1
Solution
Step 1: Check curl command correctness for health check
Without --fail, curl returns success even if HTTP status is error (like 500).Step 2: Understand impact on Docker health status
Docker will wrongly mark container healthy because curl does not fail on bad HTTP codes.Final Answer:
Missing --fail option in curl to detect HTTP errors -> Option CQuick Check:
curl needs --fail to detect HTTP errors [OK]
- Using RUN instead of CMD in HEALTHCHECK
- Wrong health endpoint URL
- Forgetting port number in URL
You want Docker to check your Spring Boot app health every 30 seconds, retry 3 times before marking unhealthy, and start checking only after 10 seconds of container start. Which HEALTHCHECK instruction correctly implements this?
Solution
Step 1: Match timing parameters to requirements
Interval 30s means check every 30 seconds, start-period 10s delays first check, retries 3 means try 3 times before unhealthy.Step 2: Verify curl command correctness
curl uses --fail to detect HTTP errors, with exit 1 to signal failure to Docker.Final Answer:
HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 CMD curl --fail http://localhost:8080/actuator/health || exit 1 -> Option AQuick Check:
Correct timing + curl --fail = HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 CMD curl --fail http://localhost:8080/actuator/health || exit 1 [OK]
- Mixing up interval and timeout values
- Omitting --fail in curl command
- Not setting start-period to delay checks
Ready to prove it?
Pass the test and this pattern moves unknown
