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

6

21

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.

Diuresis answered 15/8, 2019 at 19:46 Comment(2)
Spring Boot Actuator may be a good choice.Amparoampelopsis
@Amparoampelopsis by using spring Boot Actuator you can only check health. I need to bounce an application too if it is in stop state. so what is the best solution for this .and the application is running in docker containerDiuresis
F
29

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)

Faro answered 17/12, 2019 at 22:39 Comment(10)
hello, I tried your answer but I always have status starting and after all attempts my services failed because I never achieve healthy. Can you tell me what could be wrong? I copy your healthcheck just change port.Cumulus
and what are the logs of your application ? Did you have application started? Or did it restart ?Freebooter
and when executing the curl with the container url, what did you get as answer ?Freebooter
thank you for your effort, I found solution. curl was not installed because I use too lightweight docker imageCumulus
curl is often not installed in many images, and is also mentioned as an anti-pattern for use with the healthcheck component of docker-compose.Gratia
@AndrewTFinnell Thank. Once I search, I didn't found many articles where it is said to not use curl. Some, like this one for example (blog.sixeyed.com/docker-healthchecks-why-not-to-use-curl-or-iwr) is not bad yet not recent, but doesn't propose alternative to call the actuator/health endpoint for spring-boot image What do you propose as alternative then ?Freebooter
The /actuator/health endpoint will return "DOWN" when your DB is unreachable. If you set the docker healthcheck to check /actuator/health, then your image will reboot when the DB is unreachable. I think this is not what we want. Perhaps /actuator/info is a better choiceGillman
@xpmatteo: in the case of DB, I rather use the DB healthcheck, for instance with Postgres test: ["CMD-SHELL", "pg_isready -U postgres"]Freebooter
By default, the unhealthy states return 503, and the healthy (and UNKNOWN) states return 200. The cURL itself is probably sufficient with grepping for "UP": curl --fail --silent localhost:8081/actuator/health.Plethoric
Good answer but why not add it to the Dockerfile? docs.docker.com/engine/reference/builder/#healthcheckNonrecognition
L
9

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
Limnology answered 22/2, 2021 at 13:16 Comment(2)
Please let me know how can we use same command while using com.bmuschko.docker-spring-boot-application in a gradle springboot app. The plugin creates the dockerfile with the configuration provided in a gradle task.Despot
It is needed to use following valid syntax: test: ["CMD-SHELL", "curl -m 5 --fail --silent localhost:8080/actuator/health | jq -eCrinkly
S
2

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.

Shatterproof answered 30/3, 2024 at 22:39 Comment(0)
C
0

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
Cuyp answered 25/2, 2022 at 22:25 Comment(0)
B
0

This works for me,

  1. Install curl

     FROM eclipse-temurin:17-jre-alpine
     RUN apk --no-cache add bash curl
    
  2. 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
    
Beirut answered 22/5, 2024 at 9:43 Comment(0)
H
-2

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

Hector answered 16/8, 2019 at 7:53 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.