docker-compose healthcheck of spring boot application with actuator

I need define healthcheck for our application which is defined as service in docker-compose file (v 2.4).

currently healthcheck configuration is:

  my-service:
    image: my-service-image:latest
    healthcheck:
      test: ["CMD", "curl", "--fail", "--silent", "http://my-service/actuator/health 2>&1 | grep UP || exit 1"]
      interval: 50s
      timeout: 5s
      retries: 5
    ports:
    - "8761:8761"

when I call this from terminal

curl http://localhost:8761/actuator/health 2>&1 | grep UP || exit 1 --fail --silent

I got response {"status":"UP"} so actuator of service apparently works.

When I check what docker says after running command docker-compose ps I can see that service already starting

my_service sh -c java $JAVA_OPTS -Dja ... Up (health: starting) 0.0.0.0:8761->8761/tcp

and after all attempts failed and set status as unhealthy and my depending services failed too.

Can you tell me how to fix my healthcheck pls?

Source: StackOverflow