How to check if the service is healthy

Hey,

I created a service in my docker swarm (17.06.0-ce):

docker service create --mode=global --name=web nginx

When I check the service, i get this information:

docker service ls
ID              NAME   MODE      REPLICAS IMAGE       PORTS
6m8rro8rwtup    web    global    4/4      nginx:latest

How can I check if the service is completly healthy? I don’t want to check with docker ps the state on every node.
If I see preplicas 4/4, does it mean that all containers of my service on all nodes are healthy (also a defined healthcheck is checked)?
In that case I just can check the replicas?

if(replicas.equals("4/4")) {
   return "service is healthy;
} else {
   return "service is unhealthy";
}
1 Like

Have you taken a look at “healthchecks” ? See https://docs.docker.com/engine/reference/commandline/service_create/#options and the options that starts with --health

I added a healthcheck to my dockerfile. I can check the status on every node with “docker ps my_application”, but I would like to check the status of the service, the command “docker service ps my_application” doesn’t tell me if my application is healty on every node.

4 Likes

I am interested in this too. I think we need to define what a healthy service is in respect to the health of task (containers running) in that service.

With ps I can see that all services has the right number of tasks running but I cannot tell if those tasks are health without checking ps or logs.

It seems to me I have to find out the node for each task, then ssh there and inspect the docker container to check the health status of each task.

Any solution to this? To check the health status of a service (not individually each running container in the service)…

I wrote a script that determines the running containers for each service and which hosts they are on and then connects to that host with ssh and does a docker ps to get the status and health report.

It then reports back for each service (well if you use “apply dshc service1 service2 …”) the health of each running container for that service in a list. It’s slow but does give an overview of the health of each service.

It will be problematic, of course, if the container is not running successfully and jumping from host to host. It will show, however, the distinction between containers that are running and healthy or not.

Another reformulation of the question and clunky solution script here: https://devops.stackexchange.com/questions/3643/docker-swarm-service-health-check

Isn’t is simpler to make use of Consul? (https://www.consul.io/)

I notice that if the service is not healthy according to the healthcheck the swarm will shut it down and restart it again. https://github.com/charypar/swarm-dashboard helps me visualize it.

As for healthchecks sometimes a simple nc localhost port < /dev/null is enough for what i need but I can also do a curl

And you do all this within the docker toolkit no additional tools except for that small monitoring image.

simple:

When you create the service use the opt --detach=false

like:

docker service create --mode=global --name=web --detach=false nginx

or

docker service update --detach=false name_service