Java – Docker service health checks are not available for spring boot applications

Docker service health checks are not available for spring boot applications… here is a solution to the problem.

Docker service health checks are not available for spring boot applications

I’m running my Spring Boot web application as a docker service and it works fine without health checks.

But when I try to create a docker service with a health check option, my service automatically restarts after a while (probably after the health check times out) and it never is stable.

I created the \health rest endpoint and just returned ok
This is the command I used to create a service with and without health check options.

Command with health check options:

sudo docker service create \
    --health-cmd="curl --silent --fail localhost:8090/health || exit 1" \
    --health-interval=5s \
    --health-retries=12 \
    --health-timeout=2s \
    --health-start-period=60s \
    --name=my-service \
    -p "8090:8090" \
    my-docker-img

Output:

s43fbah1qdlxb01s4x5veecos
overall progress: 0 out of 1 tasks 
1/1: starting  [============================================>      ] 

There is no order for health check:

sudo docker service create \
    --name=my-service \
    -p "8090:8090" \
    my-docker-img

My Spring Boot application takes 20 seconds to get up and running.

Solution

Make sure “curl” is installed in the container.
-health-cmd="curl --silent --fail localhost:8090/health || exit 1" may fail. Everything else looks good.

The following commands will help you debug the issue. You will be able to see the log:

sudo docker inspect --format='{{json . State.Health}}' [container-name-or-id]

Related Problems and Solutions