Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do a health check of a Spring Boot application running in a Docker Container?

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.

like image 521
Sandeep muthyapu Avatar asked Aug 15 '19 19:08

Sandeep muthyapu


People also ask

How can I check the health status of a Docker container?

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 .

How do I monitor my Spring boot application?

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.

How do I add a health check in Docker-compose?

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.


2 Answers

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)

like image 166
LE GALL Benoît Avatar answered Nov 09 '22 02:11

LE GALL Benoît


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
like image 28
Furetto Avatar answered Nov 09 '22 02:11

Furetto