Skip to content

Commit

Permalink
Cross-compile for arm64 and mount go cache
Browse files Browse the repository at this point in the history
Closes #29

Provides is a very efficient way to build multi-arch images:
https://www.docker.com/blog/faster-multi-platform-builds-dockerfile-cross-compilation-guide/
  • Loading branch information
vaskozl committed May 24, 2024
1 parent 081163b commit abf99c2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
20 changes: 15 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
ARG GOARCH="amd64"
FROM golang:1.22 AS builder
FROM --platform=$BUILDPLATFORM golang:1.22 AS builder

WORKDIR /src

COPY go.mod go.sum .
RUN --mount=type=cache,target=/go/pkg \
go mod download

COPY . .
# build
RUN go mod download
RUN CGO_ENABLED=0 go build -o /go/bin/netpol ./cmd

ARG TARGETOS TARGETARCH
RUN --mount=type=cache,target=/root/.cache/go-build \
--mount=type=cache,target=/go/pkg \
CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETARCH \
go build -o /go/bin/netpol ./cmd

# STEP 2: Build small image
FROM registry.k8s.io/build-image/distroless-iptables:v0.5.2
COPY --from=builder --chown=root:root /go/bin/netpol /bin/netpol

CMD ["/bin/netpol"]
15 changes: 11 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,16 @@ REGISTRY?=gcr.io/k8s-staging-networking
TAG?=$(shell echo "$$(date +v%Y%m%d)-$$(git describe --always --dirty)")
# the full image tag
IMAGE?=$(REGISTRY)/$(IMAGE_NAME):$(TAG)
PLATFORMS?=linux/amd64,linux/arm64

# required to enable buildx
export DOCKER_CLI_EXPERIMENTAL=enabled
image-build:
# docker buildx build --platform=${PLATFORMS} $(OUTPUT) --progress=$(PROGRESS) -t ${IMAGE} --pull $(EXTRA_BUILD_OPT) .
docker build . -t ${IMAGE}
docker buildx build . \
--platform="${PLATFORMS}" \
--tag="${IMAGE}" \
--load

image-push:
docker buildx build . \
--platform="${PLATFORMS}" \
--tag="${IMAGE}" \
--push

0 comments on commit abf99c2

Please sign in to comment.