healthcheck
A very simple Docker Health Check helper.
Description
This small project can be used in Docker files to monitor the Docker container health by checking
an HTTP GET endpoint. If the endpoint is not responsive within 2 seconds or returns an HTTP status
code < 200, or an HTTP code > 299, the healthcheck executable (hc) will end with exit code 1.
Otherwise, exit code 0 is used.
Usage
Download the executable from here.
Binary usage:
hc <url to check>
- url to check: the full HTTP GET endpoint address that will be queried
- exit code: 0 on success, 1 on failure
Docker usage:
COPY --from=buildImage /build/hc /app/hc
HEALTHCHECK --interval=30s --timeout=5s --start-period=30s --retries=3 CMD [ "/app/hc", "http://www.url.to.check.com" ]
Health check service library
This project also provides a very simple health check library that can be used to set up the health check server.
This is an example of how to use the library:
package main
import "git.prolicht.digital/golib/healthcheck"
func main() {
healthcheck.New().Start() // start a default health check service on port 11223, this function is async
healthcheck.New().StartForeground(context.Background()) // start a default health check service on port 11223 in foreground
healthcheck.New(healthcheck.WithCustomCheck(func() int {
return 500 // this is the HTTP status code
})).Start() // start a custom health check service
healthcheck.New(healthcheck.ListenOn(":8080"), healthcheck.WithCustomCheck(func() int {
return 500
})).Start() // start a custom health check service on port 8080
}
Using the healthcheck.ListenOnFromEnv()
option reads the listening address from an environment variable.
The default environment variable (HC_LISTEN_ADDR) name can be overridden by passing the name as
argument: healthcheck.ListenOnFromEnv("THE_ENVIRONMENT_VARIABLE")
.