Skip to content

Commit e9a94a5

Browse files
Merge pull request #243 from numtide/perf/native-cross-compile
perf(docker): use native cross-compilation instead of QEMU
2 parents 9ace21c + 110632b commit e9a94a5

File tree

2 files changed

+10
-21
lines changed

2 files changed

+10
-21
lines changed

Dockerfile

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# Github workflow step anchore/scan-action scans only the final image
44
# sync this intermediate FROM reference with:
55
# scan-intermediate-image.yaml
6-
FROM golang:1.25.7-alpine3.23 AS builder
6+
FROM --platform=$BUILDPLATFORM golang:1.25.7-alpine3.23 AS builder
77

88
ARG TARGETOS
99
ARG TARGETARCH
@@ -19,11 +19,9 @@ RUN go mod download
1919
# Copy the Go source (relies on .dockerignore to filter)
2020
COPY . .
2121

22-
# Build
23-
# the GOARCH has no default value to allow the binary to be built according to the host where the command
24-
# was called. For example, if we call make docker-build in a local env which has the Apple Silicon M1 SO
25-
# the docker BUILDPLATFORM arg will be linux/arm64 when for Apple x86 it will be linux/amd64. Therefore,
26-
# by leaving it empty we can ensure that the container and binary shipped on it will have the same platform.
22+
# Build using Go's native cross-compiler (CGO_ENABLED=0).
23+
# --platform=$BUILDPLATFORM on the FROM makes this stage run on the host arch
24+
# (amd64) even when targeting arm64, avoiding slow QEMU emulation.
2725
RUN CGO_ENABLED=0 \
2826
GOOS=${TARGETOS:-linux} \
2927
GOARCH=${TARGETARCH} \

Makefile

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -266,9 +266,8 @@ build: manifests generate fmt vet ## Build manager binary with version metadata
266266
run: manifests generate fmt vet ## Run a controller from your host.
267267
go run ./cmd/multigres-operator/main.go
268268

269-
# If you wish to build the manager image targeting other platforms you can use the --platform flag.
270-
# (i.e. docker build --platform linux/arm64). However, you must enable docker buildKit for it.
271-
# More info: https://docs.docker.com/develop/develop-images/build_enhancements/
269+
# Cross-platform builds are handled natively via --platform=$BUILDPLATFORM in the Dockerfile.
270+
# Use docker-buildx target or pass --platform to docker build for multi-arch images.
272271
.PHONY: container
273272
container: ## Build container image
274273
$(CONTAINER_TOOL) build -t ${IMG} .
@@ -281,22 +280,14 @@ minikube-load:
281280
container-push: ## Push container image
282281
$(CONTAINER_TOOL) push ${IMG}
283282

284-
# PLATFORMS defines the target platforms for the manager image be built to provide support to multiple
285-
# architectures. (i.e. make docker-buildx IMG=myregistry/mypoperator:0.0.1). To use this option you need to:
286-
# - be able to use docker buildx. More info: https://docs.docker.com/build/buildx/
287-
# - have enabled BuildKit. More info: https://docs.docker.com/develop/develop-images/build_enhancements/
288-
# - be able to push the image to your registry (i.e. if you do not set a valid value via IMG=<myregistry/image:<tag>> then the export will fail)
289-
# To adequately provide solutions that are compatible with multiple platforms, you should consider using this option.
290-
PLATFORMS ?= linux/arm64,linux/amd64,linux/s390x,linux/ppc64le
283+
# PLATFORMS defines the target platforms for the manager image.
284+
PLATFORMS ?= linux/arm64,linux/amd64
291285
.PHONY: docker-buildx
292-
docker-buildx: ## Build and push docker image for the manager for cross-platform support
293-
# copy existing Dockerfile and insert --platform=${BUILDPLATFORM} into Dockerfile.cross, and preserve the original Dockerfile
294-
sed -e '1 s/\(^FROM\)/FROM --platform=\$$\{BUILDPLATFORM\}/; t' -e ' 1,// s//FROM --platform=\$$\{BUILDPLATFORM\}/' Dockerfile > Dockerfile.cross
286+
docker-buildx: ## Build and push docker image for cross-platform support
295287
- $(CONTAINER_TOOL) buildx create --name multigres-operator-builder
296288
$(CONTAINER_TOOL) buildx use multigres-operator-builder
297-
- $(CONTAINER_TOOL) buildx build --push --platform=$(PLATFORMS) --tag ${IMG} -f Dockerfile.cross .
289+
- $(CONTAINER_TOOL) buildx build --push --platform=$(PLATFORMS) --tag ${IMG} .
298290
- $(CONTAINER_TOOL) buildx rm multigres-operator-builder
299-
rm Dockerfile.cross
300291

301292
.PHONY: build-installer
302293
build-installer: manifests generate kustomize ## Generate a consolidated YAML with CRDs and deployment.

0 commit comments

Comments
 (0)