Reputation: 3690
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?
Upvotes: 12
Views: 9097
Reputation: 13898
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.
Upvotes: 11
Reputation: 1
exit 1 is the response code for the healthcheck 0 - success 1 - unhealthy 2 - reserved
Upvotes: -1
Reputation: 692
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)
Upvotes: 2