For Docker Swarm. If a container is unhealthy based on the health check, the health check as documented would return 1 as the exit code.
My question when the container receives the SIGTERM from Docker Swarm. Should it return 0 or non-0? From my understanding docker also sends SIGTERM when you stop a container and in that case it should return 0 since it cleanly shuts down.
However, with that the on-failurerestart-policy does not appear to work correctly because the container completes cleanly. I mean I can run it as always but I was wondering if there was a better way.
The healtcheck itself could return 1 (or any non-zero code) to indicate a failure in the healtcheck, but that is not the failure of the process in the container, so it will not change the exit code of the main process. The healthcheck is like an external service checking the health periodically. So I would say you need “always” for restart policy to restart the container when the health check kills it.
I’m not a regular swarm user, so someone may have a better answer later, but in general, that behavior seems correct.
Like @rimelek said: only the exit code is relevant for the restart policy. Regardless whether it’s a swarm task or a plain container.
With docker compose, health checks have an effect on depends_on when a depending service is expected to wait for another container to be healthy. An unhealthy container just tracks the state, without any real consequence in regards of restarts. Only with swarm services, failed health checks have an impact and terminate unhealthy service tasks.
I doubt that a gracefully terminated container would restart with the restart policy on-failure. I assume that’s why the restart policy unless-stopped is not implemented for swarm services, as there is no way to distinguish whether a SIGTERM was triggered by a failed health check or docker stop.
Though I would expect a swarm service to restart with on-failure, if the graceful termination triggered by SIGTERM failed, and the service task was killed with SIGKILL.
I am with @rimelek regarding the restart policy: use always