Demo of Docker HEALTHCHECK for a service

Hey, I’ve put together a little demo showing of the Docker HEALTHCHECK functionality, which can help keep your services running fine. Docker can periodically run a command in your container and depending on the command’s results restart it.

In this demo, a small web service is implemented, which has a state endpoint. Ona a given trigger, it will go into a bad state, and starts to return 503 errors on that endpoint. The healthcheck picks that up, and restarts the container.

Of course this is a very simple demo, and there are many different ways to check health (presence or absence of files, simple results of a complex script packaged with the service, network pings, etc…)

Hope this will help to make your services work even more reliably!

1 Like

Here’s also a version using a SenseHAT (which is more physical, but do need extra hardware compared to the above).

It uses lockfiles (and checking for their existence) for healthcheck. This is of course only one way, and not necessarily appropriate in all cases, just one way…>

HEALTHCHECK --start-period=5s --timeout=2s --interval=3s --retries=1 \
    CMD if [ ! -e "/tmp/working" ]; then exit 1; fi

Something like this (though I should have shortened the healthcheck wait times for this video :stuck_out_tongue: , also --retries cannot be less than 1).