Hi All, this document deals with how to add health check for Docker containers.
Since most of the application uses docker for running the applications, It is always required to monitor the health of the docker containers to see the application inside is running or not.
There are certain cases where docker containers run whereas the application inside the docker container won’t. So using the health check would be able to understand the application is running within the container.
Healthcheck commands try to retrieve Response 200 from the application service using curl command, if the response is 200 then it’s container is healthy, otherwise unhealthy.
Here is an example of how I have enabled health check for a sample nodejs application. You can get the source code for the application from this Link.
Enabling docker health check has to be done in Dockerfile using the keyword “HEALTHCHECK”.
Here is the sample dockerfile command where health check is enabled.
Build the container with health check enabled
Run the docker container
Now you can see in the status health: starting, once the docker container is Up and running the status gets changed to healthy.
Implementing health check will more useful when using docker swarm, so containers can be restarted whenever the manager identifies container is unhealthy.
It is always better to create a module in the application to check health check status.
© 2019, Techrunnr. All rights reserved.