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.
This commit is contained in:
Hector 2021-02-08 19:48:55 +00:00
parent 50c969014f
commit b63f641bfd
2 changed files with 22 additions and 4 deletions

View file

@ -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

View file

@ -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) .