Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Health check - web

Currently I am trying to check if a webservice running inside a docker container is healthy and if its HTTP status code is 200.

But dockers built in healthcheck only checks for exit codes.

I am running this command via terminal:

curl -o /dev/null -s -w "%{http_code}\n" http://localhost:8080/api/health

And check if the returned status code is 200. How can I embed this one inside dockers healthcheck?

like image 404
Creative crypter Avatar asked Aug 30 '25 16:08

Creative crypter


1 Answers

You can work with the fact that HEALTHCHECK only checks the exit code values of a command as follows:

  1. Use the -w and -s options in curl to only output the http status code of the api request
  2. Use bash test expressions to check the status code

The HEALTHCHECK in your Dockerfile might look like this:

HEALTHCHECK --interval=20s --retries=2 CMD \
    [[ "$(curl -o /dev/null -s -w "%{http_code}\n" http://localhost:8080/api/health)" == "200" ]]

See documentation here

like image 87
moebius Avatar answered Sep 02 '25 06:09

moebius