'What happens to a Docker Container when HEALTHCHECK fails

The docker docs say what a HEALTHCHECK instruction is and how to check the health of a container. But I am not able to figure out what happens when healthcheck fails. Like will the container be restarted or stoped or any of these two as per user instruction.

Further the example quoted is:
HEALTHCHECK --interval=5m --timeout=3s CMD curl -f http://localhost/ || exit 1

What is the exit 1 about?



Solution 1:[1]

When running HEALTHCKECKS you can specify:

  • --interval=DURATION (default 30s)

  • --timeout=DURATION (default 30s)

  • --retries=N (default 3)

And the container can have three states:

  • starting – Initial status when the container is still starting.

  • healthy – When the command succeeds.

  • unhealthy – When a single run of the HEALTHCHECK takes longer than the specified timeout. When this happens it will run retries and will be declared "unhealthy" if it still fails.

When the check fails for a specified number of times in a row, the failed container will:

  • stay in "unhealthy" state if it is in standaolne mode

  • restart if it is in Swarm mode

Otherwise it will exit with error code 0 which means it is considered "healthy".

I hope it makes things more clear.

Solution 2:[2]

There is a good video that i have seen and the guy explains pretty well

https://youtu.be/dLBGoaMz7dQ

basically let's say you are running a web server in production and it has 3 replicas. during the deployment you want to make sure that you don't lose any of the requests. the HEALTHCHECK basically helps with identifying when the server is actually running. it takes like a second or two for your server to start listening and in that time window you can lose requests.

by using HEALTHCHECKS you can make sure that the server is running, that is why sometimes people use CURL (not a best practice)

Solution 3:[3]

exit 1 is the response code for the healthcheck 0 - success 1 - unhealthy 2 - reserved

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Franklin Piat
Solution 2 Eitank
Solution 3 Vignesh