Skip to content

Commit

Permalink
Use tonistiigi/xx for cross-platform builds
Browse files Browse the repository at this point in the history
Update the Dockerfile to use tonistiigi/xx to cross-compile the
manager binaries natively and speed up the multi-arch image build
time.

Signed-off-by: Sunny <darkowlzz@protonmail.com>
  • Loading branch information
darkowlzz committed Oct 11, 2021
1 parent ef6e775 commit 1a2bc5d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
15 changes: 13 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
ARG XX_VERSION=1.0.0-rc.2

FROM --platform=$BUILDPLATFORM tonistiigi/xx:${XX_VERSION} AS xx

# Build the manager binary
FROM golang:1.16-alpine as builder
FROM --platform=$BUILDPLATFORM golang:1.16-alpine AS builder

# Copy the build utilities.
COPY --from=xx / /

ARG TARGETPLATFORM

# Configure workspace.
WORKDIR /workspace

# copy modules manifests
Expand All @@ -20,7 +30,8 @@ COPY controllers/ controllers/
COPY internal/ internal/

# build without giving the arch, so that it gets it from the machine
RUN CGO_ENABLED=0 go build -a -o image-reflector-controller main.go
ENV CGO_ENABLED=0
RUN xx-go build -a -o image-reflector-controller main.go

FROM alpine:3.13

Expand Down
10 changes: 9 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ CRD_OPTIONS ?= crd:crdVersions=v1
ENVTEST_BIN_VERSION?=1.22.0
KUBEBUILDER_ASSETS?=$(shell $(SETUP_ENVTEST) use -i $(ENVTEST_BIN_VERSION) -p path)

# Allows for defining additional Docker buildx arguments, e.g. '--push'.
BUILD_ARGS ?=
# Architectures to build images for.
BUILD_PLATFORMS ?= linux/amd64,linux/arm64,linux/arm/v7

# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
ifeq (,$(shell go env GOBIN))
GOBIN=$(shell go env GOPATH)/bin
Expand Down Expand Up @@ -77,7 +82,10 @@ generate: controller-gen

# Build the docker image
docker-build: test
docker build . -t ${IMG}
docker buildx build \
--platform=$(BUILD_PLATFORMS) \
-t ${IMG} \
${BUILD_ARGS} .

# Push the docker image
docker-push:
Expand Down

0 comments on commit 1a2bc5d

Please sign in to comment.