docker-postfix/postfix-exporter-01.patch
2025-01-30 20:26:16 +01:00

51 lines
1.8 KiB
Diff

diff --git a/Dockerfile b/Dockerfile
index 75a22d8..81145c9 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -1,8 +1,41 @@
-FROM scratch
+# syntax=docker/dockerfile:1.2
+FROM --platform=$BUILDPLATFORM golang:1.23 AS builder
LABEL org.opencontainers.image.source="https://github.com/hsn723/postfix_exporter" \
org.opencontainers.image.authors="Hsn723" \
org.opencontainers.image.title="postfix_exporter"
+ARG TARGETPLATFORM
+ARG TARGETOS
+ARG TARGETARCH
+ARG TARGETVARIANT
+
+WORKDIR /src
+
+# avoid downloading the dependencies on successive builds
+RUN apt-get update -qq && apt-get install -qqy \
+ build-essential \
+ libsystemd-dev
+
+COPY go.mod go.sum ./
+RUN go mod download
+RUN go mod verify
+
+COPY . .
+
+# Force the go compiler to use modules
+ENV GO111MODULE=on
+# go test fails (sometimes) because it relies on an external dependency:
+#
+# warning: SASL authentication failure: cannot connect to saslauthd server: Permission denied
+# warning: SASL authentication failure: Password verification failed
+# 732BB407C3: host mail.domain.com[1.1.1.1] said: 451 DT:SPM 163 mx13,P8CowECpNVM_oEVaenoEAQ--.23796S3 1514512449, please try again 15min later (in reply to end of DATA command)
+#
+# Since we are checking out a specific SHA hash and we know this tests have worked previously, we are quite certain that the code will work.
+# Hence disabling the test here.
+# RUN go test
+RUN echo "I am running on $BUILDPLATFORM, building for $TARGETPLATFORM $TARGETOS/$TARGETARCH/$TARGETVARIANT"
+RUN env GOOS="${TARGETOS}" GOARCH="${TARGETARCH}" GOARM="$( echo "$TARGETVARIANT" | grep -E -o "\\d+$")" go build -ldflags '-extldflags "-static"' -o /bin/postfix_exporter
+
+FROM scratch
EXPOSE 9154
-COPY postfix_exporter /
-COPY LICENSE /
-ENTRYPOINT ["/postfix_exporter"]
+WORKDIR /
+COPY --from=builder /bin/postfix_exporter /bin/
+ENTRYPOINT ["/bin/postfix_exporter"]