traefik-forward-auth icon indicating copy to clipboard operation
traefik-forward-auth copied to clipboard

Unable to add healthcheck

Open mbw2001 opened this issue 3 years ago • 2 comments

I often see this container fail after a server reboot, which in turn makes all containers using this fail. If I restart this container all is well.

I would like to implement a simple healthcheck like:

healthcheck:
    test: ["CMD", "wget", "-nv -t1 --spider 'http://localhost:4181/' || exit 1"]
    interval: 30s
    timeout: 10s
    retries: 5

But I don't know how to implement something like this for this baseimage as I cannot get access to any sort of shell, wget, curl or similar.

A healthcheck implementet directly in the image would be great, but if anyone successfully added their own in docker-compose that would work as well.

mbw2001 avatar Mar 16 '21 14:03 mbw2001

These guys have implemented a quite simple solution: https://github.com/Soluto/golang-docker-healthcheck-example

Rebuilding the container with the following Dockerfile works:

FROM golang:1.13-alpine as builder

# Setup
RUN mkdir -p /go/src/github.com/thomseddon/traefik-forward-auth
WORKDIR /go/src/github.com/thomseddon/traefik-forward-auth

# Add libraries
RUN apk add --no-cache git

# Copy & build
ADD . /go/src/github.com/thomseddon/traefik-forward-auth/
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -a -installsuffix nocgo -o /traefik-forward-auth github.com/thomseddon/traefik-forward-auth/cmd
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -a -installsuffix nocgo -o /healthcheck github.com/Soluto/golang-docker-healthcheck/healthcheck

# Copy into scratch container
FROM scratch
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
COPY --from=builder /traefik-forward-auth ./
COPY --from=builder /healthcheck ./healthcheck
ENTRYPOINT ["./traefik-forward-auth"]

HEALTHCHECK --interval=1s --timeout=1s --start-period=2s --retries=3 CMD [ "/healthcheck" ]

But of course it would be better to simply add the file to the existing repo, rather than depend on a third party repo. The file is very simple:

package main

import (
	"fmt"
	"net/http"
	"os"
)

func main() {
	_, err := http.Get(fmt.Sprintf("http://127.0.0.1:%s/health", os.Getenv("PORT")))
	if err != nil {
		os.Exit(1)
	}
}

Because of the implementation I had to add an environment variable PORT to match the current port, so adding to this repo and using the existing variable for port would be better.

mbw2001 avatar Mar 16 '21 14:03 mbw2001

See https://github.com/thomseddon/traefik-forward-auth/discussions/209

kaysond avatar Mar 29 '21 22:03 kaysond