How to Docker healthcheck a Go HTTP server

https://www.reddit.com/r/golang/comments/bi1ry0/how_to_docker_healthcheck_a_go_http_server/ by

Hi all,

I am currently searching for the way to serve a healthcheck on a Go HTTP server, without exposing it to outside users. I want this to hide implementation details from the user and, in case the healthcheck consumes some resources, keep it internal for Docker to use it.

I initially had a route /healthcheck on my server that would be exposed to all interfaces (0.0.0.0). I then decided to have a healthcheck server in its goroutine listening on localhost only (127.0.0.1). However this approach has the problem that if the main public server fails, the healthcheck might still succeed.

How would you solve the problem of having a healthcheck server without exposing it to the outside world?