Configuring Docker Health Check for Go Web Applications
Docker has been providing a health check mechanism for quite some time. It is useful in identifying issues with programs that can fail in ways other than just outright crashing.
And it is easy to set up.
Docker health checks work periodically running a program within the container and observing its exit status. If it exits with a 0, the container is considered healthy. If it exits with a 1, the container is considered to be unhealthy.
You can configure health check within the Dockerfile or when starting/creating a container using docker run
/docker create
.
Let’s begin with a two-staged build+run Dockerfile for a simple Go web application:
|
|
Within your Go program, handle the /api/health_checks/ready
HTTP path:
|
|
You can now build and run the Docker container:
|
|
Alternatively, you can configure health check when running the container (instead of in the Dockerfile):
docker run -P --health-interval=30s --health-timeout=3s --health-cmd curl -f http://127.0.0.1:8080/api/health_checks/ready || exit 1 hellohealth
Either way, checking the status of the container you will notice that it is now reporting its health as “starting”.
» docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
08b127b45803 hellohealth "./hellohealth" 4 seconds ago Up 2 seconds (health: starting) 0.0.0.0:32769->8080/tcp, :::32769->8080/tcp elegant_colden
But after a few seconds it will change to “healthy”.
» docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
08b127b45803 hellohealth "./hellohealth" 37 seconds ago Up 35 seconds (healthy) 0.0.0.0:32769->8080/tcp, :::32769->8080/tcp elegant_colden
A somewhat complete example of this tutorial is available in this GitHub repository.
This post is 33rd of my #100DaysToOffload challenge. Want to get involved? Find out more at 100daystooffload.com.
comments powered by Disqus