How to integrate healthcheck with docker swarm?

I am trying to integrate healthcheck with docker swarm. I have created my own custom health check API to monitor the health of container.
I am using following tag for healthcheck in yaml file:

healthcheck:
      test: curl -X GET -sf https://${HOSTNAME}:port/module_name/health || exit 0
      interval: 30s
      timeout: 3s
      retries: 25 

Here I have tried with different API responses like json object, 200 response, 500 response also tried with 0 and 1 returning value. But in each case docker is not able to understand the response of my health API. The behavior of container totally depends on whatever value I mentioned in test tag’s exit code if I set exit 0 container is always healthy or if set exit 1 container is always unhealthy.

How the healthcheck will understand my custom API response?
Ex: If my custom API returns 500 or 1 then healthcheck should consider this as there is something wrong and need to mark container as unhealthy.

Can anyone help me to understand how to use healthcheck with docker-swarm?

Hi, you probably just need to change the fallback to exit 1, i.e.

healthcheck:
   test: curl -X GET -sf https://${HOSTNAME}:port/module_name/health || exit 1

otherwise your code looks OK, double-check your URL.

Documentation: https://docs.docker.com/compose/compose-file/#healthcheck