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.
Hence, docker health check command will return healthy if the exit code is 0 and returns unhealthy if the exit code is 1. You can check the container's health by running the command docker-compose up -d . Check the list of containers using the command sudo docker ps -a .
The first part of monitoring Spring Boot applications is choosing a metrics database. By default, Spring Boot will configure a Micrometer metrics registry in every application. This default implementation collects a pre-defined set of application metrics such as memory and CPU usage, HTTP requests, and a few others.
Basics: Docker healthcheck In docker-compose, it is written like following. The server container relies on db container. db should have healthcheck section and the condition. server should have db in the depends_on and the condition underneath to make sure that it boots after the db container gets health status.
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)
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With