From b63f641bfd7d6ece4e5cae6183e9caa46b405bec Mon Sep 17 00:00:00 2001 From: Hector Date: Mon, 8 Feb 2021 19:48:55 +0000 Subject: [PATCH] build: compile tool during docker build Update the project Dockerfile to compile the tool during the docker build instead of assuming the goreleaser tool was run previously. This allows for custom data to be set in the compiled tool (e.g. compiled by docker) and removes the dependency on another build step. Update the Makefile to include a new command to build the tool for docker which sets the version data correctly. Rename the docker commands to follow the `docker/build-...` format to avoid confusion with the build commands. --- Dockerfile | 20 +++++++++++++++++--- Makefile | 6 +++++- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/Dockerfile b/Dockerfile index 7332a96..61bcd90 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,10 +1,24 @@ # Using golang:latest instead of alpine because of issues with sqlite3 FROM golang:latest +# Create build folder to compile tool +WORKDIR /build + +# Copy source files to build folder and link to the /go folder +COPY . /build +RUN ln -s /go/src/ /build/src + +# Compile the tool using a Make command +RUN make build/docker + +# Create main app folder to run from WORKDIR /app -COPY dist/fail2ban-prometheus-exporter_linux_amd64/fail2ban-prometheus-exporter /app -COPY docker/run.sh /app +# Move compiled binary to app folder and delete build folder +RUN mv /build/src/exporter /app/fail2ban-prometheus-exporter +RUN rm -rf /build + +# Copy init script into main app folder and set as entry point +COPY docker/run.sh /app/ RUN chmod +x /app/* - ENTRYPOINT /app/run.sh diff --git a/Makefile b/Makefile index 3f57577..a48b4bb 100644 --- a/Makefile +++ b/Makefile @@ -24,7 +24,11 @@ build/release: ./tools/goreleaser_linux_amd64 --rm-dist --skip-publish build/docker: + cd src/ && go build -o exporter \ + -ldflags '-X main.version=$(shell git describe --tags) -X main.commit=${shell git rev-parse HEAD} -X "main.date=${shell date --rfc-3339=seconds}" -X main.builtBy=docker' exporter.go + +docker/build-latest: docker build -t registry.gitlab.com/hectorjsmith/fail2ban-prometheus-exporter:latest . -build/docker-tag: +docker/build-tag: docker build -t registry.gitlab.com/hectorjsmith/fail2ban-prometheus-exporter:$(shell git describe --tags) .