Repository navigation
Expand file tree
/
Copy pathDockerfile
More file actions
73 lines (64 loc) · 3.45 KB
/
Copy pathDockerfile
File metadata and controls
73 lines (64 loc) · 3.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# Build on the native runner architecture and cross-compile from there.
# Without --platform=$BUILDPLATFORM this stage runs under QEMU for arm64,
# arm/v7 and arm/v6, which is what made a release build take over an hour.
# Go cross-compiles fine and CGO is off, so emulation buys nothing.
FROM --platform=$BUILDPLATFORM golang:1.27.3-alpine@sha256:d200d2dd76b03a99b52651fd1325aff7dcd35422c35c01bac56571b0507f3deb AS builder
ARG RELEASE_VERSION="dev"
ARG GIT_COMMIT="none"
ARG BUILD_DATE="unknown"
# Extra Go build tags. Empty for every release and publish image. Only the
# e2e image build sets it (to licensetest); scripts/check-release-consistency.sh
# fails when the default or a release workflow changes that.
ARG GO_BUILD_TAGS=""
# Supplied by buildx for each requested platform.
ARG TARGETOS
ARG TARGETARCH
ARG TARGETVARIANT
ARG SOURCE_DATE_EPOCH="0"
WORKDIR /build
# Dependencies change far less often than source, so keep them in their own
# layer. This RUN references no TARGET* args, so BuildKit runs it once and
# shares the result across all four platforms.
COPY go.mod go.sum /build/
RUN --mount=type=cache,target=/go/pkg/mod \
go mod download
COPY . /build/
# No -a: it forces a rebuild of every package including the standard library
# on every run, which makes Go's build cache useless. -installsuffix has done
# nothing since Go 1.10. -buildvcs=false keeps Go from needing git in the
# image; the version values come from ldflags anyway.
#
# This RUN does reference TARGET* args, so BuildKit runs it once per platform.
# The mounts are left at the default sharing=shared so those four runs happen
# concurrently; sharing=locked would serialize them. GOCACHE and GOMODCACHE
# are both safe for concurrent use by multiple processes.
RUN --mount=type=cache,target=/go/pkg/mod \
--mount=type=cache,target=/root/.cache/go-build \
set -eu; \
: "${TARGETOS:?empty - buildx did not supply TARGETOS}"; \
: "${TARGETARCH:?empty - buildx did not supply TARGETARCH}"; \
export CGO_ENABLED=0 GOOS="${TARGETOS}" GOARCH="${TARGETARCH}" SOURCE_DATE_EPOCH="${SOURCE_DATE_EPOCH}"; \
if [ "${TARGETARCH}" = "arm" ]; then export GOARM="${TARGETVARIANT#v}"; fi; \
go build -trimpath -buildvcs=false ${GO_BUILD_TAGS:+-tags "${GO_BUILD_TAGS}"} \
-ldflags "-X github.com/abahmed/kwatch/internal/version.version=${RELEASE_VERSION} -X github.com/abahmed/kwatch/internal/version.gitCommitID=${GIT_COMMIT} -X github.com/abahmed/kwatch/internal/version.buildDate=${BUILD_DATE}" \
-o kwatch ./cmd/kwatch
FROM alpine:3.24@sha256:be2656061b536c1883c4034790e21d0a09c5aaaf51968b89368a3c0371ea2c9a
LABEL maintainer="Abdelrahman Ahmed <a.ahmed1026@gmail.com>"
# Keep the release identity in the image itself. The publish workflow supplies
# these values from the exact tag checkout, rather than from the workflow's
# default branch context.
ARG RELEASE_VERSION="dev"
ARG GIT_COMMIT="none"
ARG BUILD_DATE="unknown"
LABEL org.opencontainers.image.source="https://github.com/abahmed/kwatch" \
org.opencontainers.image.version="${RELEASE_VERSION}" \
org.opencontainers.image.revision="${GIT_COMMIT}" \
org.opencontainers.image.created="${BUILD_DATE}" \
org.opencontainers.image.licenses="Elastic-2.0"
RUN apk add --no-cache ca-certificates && \
adduser -D -u 1000 kwatch
COPY --from=builder /build/kwatch /bin/kwatch
# Numeric, so runAsNonRoot can be verified without runAsUser; it matches the
# chart's runAsUser and runAsGroup.
USER 1000:1000
ENTRYPOINT ["/bin/kwatch"]