Reputation: 401
I am running a Spring Boot application within a Docker container, using the Docker file to start the application within the container. How can I check the health of the Spring Boot application within the container?
If the container stops or the application is not running, I need to restart the container or application automatically based on the health check. This way, I can ensure that the Spring Boot application will always be up and running.
Upvotes: 23
Views: 32714
Reputation: 2848
Bear in mind that the Spring Boot Actuator healthcheck can/might be configured (via management.endpoint.health.show-details
property) to return full details of health of certain subcomponents rather than a single status. In this case the simple curl commands given above will return false positives. Better to explicitly extract and check just the overall status, e.g. via:
healthcheck:
test: "test $( curl --fail --silent localhost:8080/actuator/health | cut -d':' -f2 | cut -d'\"' -f2 ) = UP"
Upvotes: 0
Reputation: 99
This works for me,
Install curl
FROM eclipse-temurin:17-jre-alpine
RUN apk --no-cache add bash curl
add healthcheck in you docker file
healthcheck:
test: "curl --fail --silent localhost:9072/actuator/health/readiness | grep UP || exit 1"
interval: 20s
timeout: 3s
retries: 3
start_period: 10s
Upvotes: 1
Reputation: 2708
For those using old LTS eclipse-temurin:17-jre-alpine or current LTS eclipse-temurin:21-jre-alpine the wget
is included in default image, thus out of box healthcheck for docker compose would look like:
healthcheck:
test: "wget -T5 -qO- http://localhost:8080/actuator/health | grep UP || exit 1"
interval: 15s
timeout: 5s
retries: 5
start_period: 20s
for kubernetes it would be
spec:
containers:
...
livenessProbe:
httpGet:
path: /actuator/health
port: 8080
initialDelaySeconds: 20
periodSeconds: 15
failureThreshold: 5
timeoutSeconds: 5
The httpGet healthcheck probe will consider the application healthy when the status code is between 200 and 399. In case the application is reporting a healthy state, /actuator/health API will respond with 200 and 503 when it’s down, thus will satisfy k8s healthcheck probe.
Upvotes: 4
Reputation: 2794
My server does a redirect on index, so I use the redirect for health checks like so:
healthcheck:
test: curl -Is localhost:8080 | head -n 1 | grep 302 || exit 1
Upvotes: 0
Reputation: 379
this works for me
healthcheck:
test: curl -m 5 --silent --fail --request GET http://localhost:8080/actuator/health | jq --exit-status -n 'inputs | if has("status") then .status=="UP" else false end' > /dev/null || exit 1
interval: 10s
timeout: 2s
retries: 10
Upvotes: 10
Reputation: 7618
If you want to use the spring boot actuator/health
as a docker healthcheck, you have to add it like this on your docker-compose file:
healthcheck:
test: "curl --fail --silent localhost:8081/actuator/health | grep UP || exit 1"
interval: 20s
timeout: 5s
retries: 5
start_period: 40s
Edit:
here the port is the management.server.port
. If you don't have specified it, it should be the server.port value
(8080 by default)
Upvotes: 30
Reputation: 790
Lots of ways of doing the basics to monitor a spring boot application in standalone you would use spring boot actuator. You can expose the "management health port" on a separate port from your application server port (if you're using rest api).
https://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-endpoints.html
Just include spring actuator dependency in your pom.xml and configure it in your applicaiton.properties/.yml and this will expose the endpoints listed in the above link.
You can use docker healthcheck to check the health of your application:
https://docs.docker.com/engine/reference/builder/#healthcheck
You can set a restart policy to ensure the container restarts when it has crashed:
https://docs.docker.com/engine/reference/run/#restart-policies---restart
Upvotes: -2