Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docker healthcheck #8

Merged
merged 2 commits into from
Dec 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,18 +1,42 @@
# 1.17-alpine bug : standard_init_linux.go:228: exec user process caused: no such file or directory
FROM golang:1.17 as build-env
ARG GOLANG_VERSION=1.17

# Building custom health checker
FROM golang:$GOLANG_VERSION as health-build-env

# Copying source
WORKDIR /go/src/app
COPY ./healthcheck /go/src/app

# Installing dependencies
RUN go get -d -v ./...

# Compiling
RUN go build -o /go/bin/healthchecker

# Building bouncer
FROM golang:$GOLANG_VERSION as build-env

# Copying source
WORKDIR /go/src/app
COPY . /go/src/app

# Installing dependencies
RUN go get -d -v ./...

# Compiling
RUN go build -o /go/bin/app

FROM gcr.io/distroless/base:nonroot
COPY --from=health-build-env --chown=nonroot:nonroot /go/bin/healthchecker /
COPY --from=build-env --chown=nonroot:nonroot /go/bin/app /

# Run as a non root user.
USER nonroot

# Using custom health checker
HEALTHCHECK --interval=10s --timeout=1s \
CMD ["/healthchecker"]

# Run app
CMD ["/app"]
3 changes: 3 additions & 0 deletions healthcheck/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/fbonalair/healthcheck

go 1.17
32 changes: 32 additions & 0 deletions healthcheck/healthchecker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package main

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

/**
Simple binary to query bouncer health check route and allow use of docker container health check
For more information, see issue https://github.com/fbonalair/traefik-crowdsec-bouncer/issues/6
*/
func main() {
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}

// Calling bouncer health check
healthCheckUrl := fmt.Sprintf("http://127.0.0.1:%s/api/v1/ping", port)
resp, err := http.Get(healthCheckUrl)
if err != nil {
log.Fatal("error while requesting bouncer's health check route :", err)
}

if resp.StatusCode == http.StatusOK {
os.Exit(0)
}

os.Exit(1)
}