From f4d6598308dbf66de8da7578e8bda8e590199f6c Mon Sep 17 00:00:00 2001 From: Hector Date: Fri, 27 Oct 2023 06:01:41 +0000 Subject: [PATCH] build: add manual docker multistep build (!114) - Include two Dockerfiles in the repo: one for Goreleaser and one for manual builds - Update the Goreleaser config to use it's own Dockerfile - The Dockerfile for manual builds uses a two step process to build the exporter - this way the base system does not need Go installed https://gitlab.com/hectorjsmith/fail2ban-prometheus-exporter/-/merge_requests/114 --- .goreleaser.yml | 2 ++ Dockerfile | 21 ++++++++++++++++++--- Dockerfile.goreleaser | 15 +++++++++++++++ Makefile | 2 +- 4 files changed, 36 insertions(+), 4 deletions(-) create mode 100644 Dockerfile.goreleaser diff --git a/.goreleaser.yml b/.goreleaser.yml index b2f87e4..c4642cb 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -23,6 +23,7 @@ dockers: extra_files: - health use: buildx + dockerfile: Dockerfile.goreleaser build_flag_templates: - "--pull" - "--platform=linux/amd64" @@ -42,6 +43,7 @@ dockers: extra_files: - health use: buildx + dockerfile: Dockerfile.goreleaser build_flag_templates: - "--pull" - "--platform=linux/arm64" diff --git a/Dockerfile b/Dockerfile index cd601d6..a9ae20c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,11 +1,26 @@ +FROM golang:1.20-buster AS build + +# Create build workspace folder +WORKDIR /workspace +ADD . /workspace + +# Install updates and build tools +RUN apt update --yes && \ + apt install --yes build-essential + +# Build the actual binary +RUN make build + +# -- -- -- -- -- -- + +# Set up image to run the tool FROM alpine # Create main app folder to run from WORKDIR /app -# Copy compiled binary to release image -# (must build the binary before running docker build) -COPY fail2ban_exporter /app/fail2ban_exporter +# Copy built binary from build image +COPY --from=build /workspace/fail2ban_exporter /app # Setup a healthcheck COPY health /app/health diff --git a/Dockerfile.goreleaser b/Dockerfile.goreleaser new file mode 100644 index 0000000..cd601d6 --- /dev/null +++ b/Dockerfile.goreleaser @@ -0,0 +1,15 @@ +FROM alpine + +# Create main app folder to run from +WORKDIR /app + +# Copy compiled binary to release image +# (must build the binary before running docker build) +COPY fail2ban_exporter /app/fail2ban_exporter + +# Setup a healthcheck +COPY health /app/health +RUN apk add curl +HEALTHCHECK --interval=10s --timeout=4s --retries=3 CMD /app/health + +ENTRYPOINT ["/app/fail2ban_exporter"] diff --git a/Makefile b/Makefile index 4421341..1d6814c 100644 --- a/Makefile +++ b/Makefile @@ -51,5 +51,5 @@ build: # Build project docker container .PHONY: build/docker -build/docker: build +build/docker: docker build -t fail2ban-prometheus-exporter .