17

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.

M. Justin
  • 14,487
  • 7
  • 91
  • 130
Sandeep muthyapu
  • 281
  • 2
  • 3
  • 8
  • Spring Boot Actuator may be a good choice. – LHCHIN Aug 16 '19 at 00:57
  • @LHCHIN 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 container – Sandeep muthyapu Aug 16 '19 at 15:37

4 Answers4

26

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)

LE GALL Benoît
  • 7,159
  • 1
  • 36
  • 48
  • 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. – Denis Stephanov Jan 21 '20 at 09:53
  • and what are the logs of your application ? Did you have application started? Or did it restart ? – LE GALL Benoît Jan 21 '20 at 13:21
  • and when executing the curl with the container url, what did you get as answer ? – LE GALL Benoît Jan 21 '20 at 15:00
  • 5
    thank you for your effort, I found solution. curl was not installed because I use too lightweight docker image – Denis Stephanov Jan 21 '20 at 17:15
  • 6
    `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. – Andrew T Finnell Mar 25 '20 at 18:03
  • 2
    @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 (https://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 ? – LE GALL Benoît Mar 26 '20 at 10:04
  • 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 choice – xpmatteo Sep 07 '20 at 08:25
  • 2
    @xpmatteo: in the case of DB, I rather use the DB healthcheck, for instance with Postgres `test: ["CMD-SHELL", "pg_isready -U postgres"]` – LE GALL Benoît Sep 07 '20 at 12:18
  • 1
    [By default](https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#actuator.endpoints.health.writing-custom-health-indicators), 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`. – M. Justin Dec 09 '21 at 20:17
  • Good answer but why not add it to the Dockerfile? https://docs.docker.com/engine/reference/builder/#healthcheck – schwaller Mar 08 '23 at 10:29
6

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
Furetto
  • 329
  • 3
  • 9
  • 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. – ABHISHEK KUMAR Mar 08 '22 at 17:06
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
Valerij Dobler
  • 1,848
  • 15
  • 25
-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

Rob Scully
  • 744
  • 5
  • 10