Docker Health Checks for Spring Boot with Actuator
Fast recall
Quick revision
Core answer
Knowledge of container health reporting and its integration with orchestration systems
Checking if the process is running rather than if it can serve requests — the JVM might be up but the app could be stuck in initialization or have failed a startup check
Explain the difference between HEALTHCHECK in Docker vs liveness/readiness probes in Kubernetes, mention spring-boot-starter-actuator's /actuator/health/liveness vs /health/readiness endpoints
Interview-ready guide
Interview answer
A Docker health check tells the container runtime whether your application is actually ready to serve traffic — not just whether the process is running. Without a health check, Docker Compose marks a container Up the moment the process starts, even if Spring Boot is still initializing.
Health check configuration
HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 CMD curl -f http://localhost:8080/actuator/health || exit 1— thestart-periodis critical for Spring Boot: it gives the JVM time to start and warm up before health checks begin counting failures- Use
/actuator/healthrather than any arbitrary endpoint — Spring Boot's health endpoint aggregates all health indicators (database connectivity, disk space, custom indicators) into one response; a simple/pingthat returns 200 OK does not verify database connectivity - Add
management.endpoint.health.probes.enabled=trueto enable Kubernetes-specific/actuator/health/livenessand/actuator/health/readinessendpoints — Spring Boot 2.3+ supports these separately, allowing different behaviors for liveness (is the JVM responding?) versus readiness (is the application ready for traffic?)
Docker Compose depends_on integration
- A healthcheck on the postgres service combined with
condition: service_healthyin the Spring Boot service'sdepends_onensures the application starts only after the database is accepting connections — prevents the most common startup failure in compose-based environments - Without the
condition: service_healthy, Compose only guarantees that the postgres container has started, not that it is ready — the Spring Boot container begins before the database and fails to establish a connection pool
Kubernetes probe alignment
- In Kubernetes, the Dockerfile HEALTHCHECK is ignored — liveness and readiness are configured directly in the pod spec; use Spring Boot Actuator's dedicated probe endpoints for both
- Make liveness more lenient than readiness: a failed liveness check restarts the pod (potentially causing a cascade), while a failed readiness check only removes the pod from the load balancer — the correct response for a temporarily overloaded pod
Docker Health Checks for Spring Boot with Actuator
Direct Answer
Use the HEALTHCHECK instruction in Dockerfile to define a command Docker runs to verify the container is healthy. For Spring Boot, check /actuator/health using wget or curl. This tells Docker Compose to wait until the app is ready before routing traffic, and lets Kubernetes know when the pod is ready to serve requests.
Common Mistakes & Pitfalls
Checking if the process is running rather than if it can serve requests — the JVM might be up but the app could be stuck in initialization or have failed a startup check
Check your understanding
Answer these without looking back at the article.
- What is the difference between liveness and readiness probes in Kubernetes?
- How do you add a custom HealthIndicator to Spring Boot Actuator?
- What happens in Kubernetes when a liveness probe fails vs a readiness probe fails?
Related interview prep categories
2Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.