What happens to a Docker Container when HEALTHCHECK fails
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 theHEALTHCHECK
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.
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)
exit 1 is the response code for the healthcheck0 - success1 - unhealthy2 - reserved