Skip to content

Commit f7b4de2

Browse files
committed
Optimize multi-arch docker image build
1 parent 0012443 commit f7b4de2

4 files changed

Lines changed: 20 additions & 15 deletions

File tree

Dockerfile

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Build the manager binary
2-
FROM golang:1.25.8-trixie AS builder
2+
FROM --platform=$BUILDPLATFORM golang:1.25.8-trixie as builder
33

44
WORKDIR /workspace/api
55
COPY api/ .
@@ -20,11 +20,13 @@ COPY controllers/ controllers/
2020
COPY utils/ utils/
2121

2222
# Build
23-
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -a -o manager main.go
23+
ARG TARGETOS
24+
ARG TARGETARCH
25+
RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH:-amd64} GO111MODULE=on go build -a -o manager main.go
2426

2527
# Use distroless as minimal base image to package the manager binary
2628
# Refer to https://github.com/GoogleContainerTools/distroless for more details
27-
FROM gcr.io/distroless/static:nonroot
29+
FROM --platform=$TARGETPLATFORM gcr.io/distroless/static:nonroot
2830
WORKDIR /
2931
COPY --from=builder /workspace/manager .
3032
USER 65532:65532

Makefile

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,11 @@ generate: controller-gen
133133

134134
# Build the docker image
135135
docker-build: test
136-
docker build --platform linux/amd64 . -t ${IMG}
136+
docker buildx build --platform linux/amd64,linux/arm64 . -t ${IMG} --load
137137

138138
# Build image for red hat certification
139139
docker-build-redhat:
140-
docker build --platform linux/amd64 -f redhat.Dockerfile . -t ${IMG} --build-arg VERSION=${VERSION} --no-cache
140+
docker buildx build --platform linux/amd64,linux/arm64 -f redhat.Dockerfile . -t ${IMG} --build-arg VERSION=${VERSION} --load --no-cache
141141

142142
# Push the docker image
143143
image-push:
@@ -171,7 +171,7 @@ bundle: yq kustomize manifests
171171
# Build the bundle image.
172172
.PHONY: bundle-build
173173
bundle-build:
174-
docker build --platform linux/amd64 -f bundle.Dockerfile -t $(BUNDLE_IMG) .
174+
docker buildx build --platform linux/amd64,linux/arm64 -f bundle.Dockerfile -t $(BUNDLE_IMG) --load .
175175

176176
.PHONY: bundle-push
177177
bundle-push: ## Push the bundle image.
@@ -187,7 +187,7 @@ rbac: manifests
187187
release: manifests kustomize crd rbac manager operator-docker-image helm-crds
188188

189189
operator-docker-image: manager test
190-
docker build --platform linux/amd64 -f operator.Dockerfile -t $(OPERATOR_IMG) .
190+
docker buildx build --platform linux/amd64,linux/arm64 -f operator.Dockerfile -t $(OPERATOR_IMG) --load .
191191
docker tag $(OPERATOR_IMG) $(OPERATOR_IMG_LATEST)
192192

193193
docker-push:
@@ -256,7 +256,7 @@ function-mesh-docker-image-name:
256256

257257
# Build the docker image without tests
258258
docker-build-skip-test:
259-
docker build --platform linux/amd64 . -t ${IMG}
259+
docker buildx build --platform linux/amd64,linux/arm64 . -t ${IMG} --load
260260

261261
e2e: skywalking-e2e yq
262262
$(E2E) run -c .ci/tests/integration/e2e.yaml
@@ -297,7 +297,7 @@ redhat-certificated-bundle: yq kustomize manifests
297297
# Build the bundle image.
298298
.PHONY: redhat-certificated-bundle-build
299299
redhat-certificated-bundle-build:
300-
docker build --platform linux/amd64 -f bundle.Dockerfile -t $(BUNDLE_IMG) .
300+
docker buildx build --platform linux/amd64,linux/arm64 -f bundle.Dockerfile -t $(BUNDLE_IMG) --load .
301301

302302
.PHONY: redhat-certificated-bundle-push
303303
redhat-certificated-bundle-push: ## Push the bundle image.
@@ -307,7 +307,7 @@ redhat-certificated-bundle-push: ## Push the bundle image.
307307
# Build the bundle image.
308308
.PHONY: redhat-certificated-image-build
309309
redhat-certificated-image-build:
310-
docker build --platform linux/amd64 -f redhat.Dockerfile . -t ${OPERATOR_IMG} --build-arg VERSION=${VERSION} --no-cache
310+
docker buildx build --platform linux/amd64,linux/arm64 -f redhat.Dockerfile . -t ${OPERATOR_IMG} --build-arg VERSION=${VERSION} --load --no-cache
311311

312312
.PHONY: redhat-certificated-image-push
313313
redhat-certificated-image-push: ## Push the bundle image.

operator.Dockerfile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM alpine:3.21
1+
FROM --platform=$TARGETPLATFORM alpine:3.21
22

33
ENV GID=10001
44
ENV UID=10000
@@ -9,6 +9,7 @@ RUN apk upgrade --no-cache \
99
&& addgroup -g $GID pulsar \
1010
&& adduser -u $UID -G pulsar -D -g '' $USER
1111

12-
ADD bin/function-mesh-controller-manager /manager
12+
ARG TARGETARCH
13+
ADD bin/function-mesh-controller-manager-${TARGETARCH:-amd64} /manager
1314

1415
USER $USER

redhat.Dockerfile

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Build the manager binary
2-
FROM golang:1.25.8-trixie as builder
2+
FROM --platform=$BUILDPLATFORM golang:1.25.8-trixie AS builder
33

44
WORKDIR /workspace/api
55
COPY api/ .
@@ -20,12 +20,14 @@ COPY controllers/ controllers/
2020
COPY utils/ utils/
2121

2222
# Build
23-
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -a -o manager main.go
23+
ARG TARGETOS
24+
ARG TARGETARCH
25+
RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH:-amd64} GO111MODULE=on go build -a -o manager main.go
2426

2527
# Use ubi image as the base image which is required by the red hat certification.
2628
# Base on the image size, the order is ubi > ubi-minimal > ubi-micro.
2729
# https://access.redhat.com/documentation/en-us/red_hat_software_certification/8.45/html/red_hat_openshift_software_certification_policy_guide/assembly-requirements-for-container-images_openshift-sw-cert-policy-introduction#con-image-metadata-requirements_openshift-sw-cert-policy-container-images
28-
FROM registry.access.redhat.com/ubi8/ubi-micro:latest
30+
FROM --platform=$TARGETPLATFORM registry.access.redhat.com/ubi8/ubi-micro:latest
2931

3032
ARG VERSION
3133

0 commit comments

Comments
 (0)