8000 ♻️ improve dockerfile by abahmed · Pull Request #480 · abahmed/kwatch · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Keep the build context to what `go build ./cmd/kwatch` actually needs.
# Without this, .git alone is 5.1 MB of a 7.8 MB repo, and every commit
# changes it — which invalidates the COPY layer on every single build.

.git
.gitignore
.github

# Docs and assets are never compiled in.
*.md
docs/
assets/

# Deployment manifests and the chart ship from the repo, not the image.
deploy/

# Tooling config.
.golangci.yml
.dockerignore
Dockerfile
LICENSE
66 changes: 66 additions & 0 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,69 @@ jobs:
- name: Template tests
if: steps.changes.outputs.chart == 'true'
run: ./deploy/chart/test_helm.sh

# publish.yml is the only other place a container image gets built, and it
# runs on a release — after the tag exists. Without this job a broken
# Dockerfile is only discovered once it is already shipping.
image:
name: Image
runs-on: ubuntu-latest
timeout-minutes: 30
permissions:
contents: read
steps:
- name: Checkout code
uses: actions/checkout@v7
- name: Detect image changes
id: changes
uses: dorny/paths-filter@v3
with:
filters: |
image:
- 'Dockerfile'
- '.dockerignore'
- 'go.mod'
- 'go.sum'
- name: Set up QEMU
if: steps.changes.outputs.image == 'true'
uses: docker/setup-qemu-action@v4
- name: Set up Docker Buildx
if: steps.changes.outputs.image == 'true'
uses: docker/setup-buildx-action@v4
# Same platform list as the release build. Nothing is pushed, and the
# cache is read-only so pull requests never add to the repo cache quota.
- name: Build every release platform
if: steps.changes.outputs.image == 'true'
uses: docker/build-push-action@v7
with:
platforms: linux/amd64,linux/arm64,linux/arm/v7,linux/arm/v6
context: .
push: false
cache-from: type=gha
build-args: |
RELEASE_VERSION=ci
GIT_COMMIT=${{ github.sha }}
BUILD_DATE=ci
# A cross-compile that silently falls back to the host would still build
# and still push, producing amd64 binaries inside arm manifests. Only
# inspecting the binary catches that.
- name: Verify each binary is built for its own architecture
if: steps.changes.outputs.image == 'true'
run: |
set -euo pipefail
check() {
docker buildx build --platform "$1" --output "type=local,dest=/tmp/$2" .
got=$(file -b "/tmp/$2/bin/kwatch")
echo "$1 -> $got"
echo "$got" | grep -q "$3" || { echo "::error::$1 produced the wrong architecture"; exit 1; }
}
check linux/amd64 amd64 "x86-64"
check linux/arm64 arm64 "ARM aarch64"
check linux/arm/v7 armv7 "ARM, EABI5"
- name: Smoke-test the image
if: steps.changes.outputs.image == 'true'
run: |
set -euo pipefail
docker buildx build --platform linux/amd64 --load -t kwatch:ci \
--build-arg RELEASE_VERSION=ci .
docker run --rm kwatch:ci --version
52 changes: 39 additions & 13 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,27 +1,53 @@
FROM golang:1.27-alpine AS builder
# 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-alpine AS builder

ARG RELEASE_VERSION="dev"
ARG GIT_COMMIT="none"
ARG BUILD_DATE="unknown"
LABEL maintainer="Abdelrahman Ahmed <a.ahmed1026@gmail.com>"

RUN apk update && \
apk add git build-base && \
rm -rf /var/cache/apk/* && \
mkdir -p "/build"
# Supplied by buildx for each requested platform.
ARG TARGETOS
ARG TARGETARCH
ARG TARGETVARIANT

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 go mod download
RUN --mount=type=cache,target=/go/pkg/mod \
go mod download

COPY . /build/
RUN CGO_ENABLED=0 GOOS=linux go build \
-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}" \
-a -installsuffix cgo -o kwatch ./cmd/kwatch

# 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}"; \
if [ "${TARGETARCH}" = "arm" ]; then export GOARM="${TARGETVARIANT#v}"; fi; \
go build -trimpath -buildvcs=false \
-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
RUN apk add --update ca-certificates && \
adduser -D -u 1000 kwatch && \
rm -rf /var/cache/apk/*
LABEL maintainer="Abdelrahman Ahmed <a.ahmed1026@gmail.com>"
RUN apk add --no-cache ca-certificates && \
adduser -D -u 1000 kwatch
COPY --from=builder /build/kwatch /bin/kwatch
USER kwatch
ENTRYPOINT ["/bin/kwatch"]
Loading
0