Health checks help Docker know if your Spring Boot app is running well or needs restarting. This keeps your app reliable and available.
Health checks in Docker in Spring Boot
HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 CMD curl -f http://localhost:8080/actuator/health || exit 1
This Dockerfile instruction tells Docker how to check your app's health.
It uses Spring Boot's built-in /actuator/health endpoint to verify the app is running.
HEALTHCHECK CMD curl -f http://localhost:8080/actuator/health || exit 1
HEALTHCHECK --interval=10s --timeout=3s CMD curl -f http://localhost:8080/actuator/health || exit 1
HEALTHCHECK --start-period=20s --retries=5 CMD curl -f http://localhost:8080/actuator/health || exit 1
This Dockerfile builds a container for a Spring Boot app. It exposes port 8080 and adds a health check that calls the app's /actuator/health endpoint every 30 seconds. If the app is unhealthy, Docker can restart it automatically.
FROM openjdk:17-jdk-slim WORKDIR /app COPY target/myapp.jar ./myapp.jar EXPOSE 8080 HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \ CMD curl -f http://localhost:8080/actuator/health || exit 1 ENTRYPOINT ["java", "-jar", "myapp.jar"]
Make sure your Spring Boot app has the actuator dependency and the health endpoint enabled.
You can customize the health endpoint or create your own for more specific checks.
Use Docker commands like docker ps and docker inspect to see health status.
Health checks let Docker watch your Spring Boot app's health automatically.
Use the /actuator/health endpoint as the check target.
Configure timing options to fit your app's startup and response times.