What happens to a Docker Container when HEALTHCHECK fails
Asked Answered
D

3

11

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?

Debutante answered 5/6, 2020 at 9:56 Comment(1)
exit 1 is to exit with error if the other command (HEALTHCHECK) didn't succeed.Pelagia
R
11

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 standalone 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.

Riotous answered 5/6, 2020 at 13:37 Comment(3)
This should be marked as the answer. Do you have any sources? For: > stay in "unhealthy" state if it is in standaolne modePeroxide
If it is in an "unhealthy" state, is the container down - can it respond to requests?Matusow
For those interested in how to respond to a unhealthy container in standalone mode, there are a few options, including running a cron job to restart unhealthy containers: stackoverflow.com/a/74014021Valle
W
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)

Wiencke answered 5/6, 2020 at 10:1 Comment(0)
T
-1

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

Tilefish answered 13/10, 2021 at 12:51 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.