Skip to content

Commit ce4b397

Browse files
committed
Fix: multiarch build by moving the binaries build stage to the golang image
Use golang:1.23 as the builder image in the Dockerfile. With the –platform=$BUILDPLATFORM flag, the image is pulled for the builder host's current architecture. Cross-compilation occurs in the builder image using TARGETOS and TARGETARCH build arguments to determine the target OS/arch. These args are set automatically by the multiarch-build process (with docker buildx). The final container image (registry.access.redhat.com/ubi9/ubi-minimal) is pulled for the correct target architecture, such as amd64 or arm64. This update fixes support for multiarch builds and speeds up image building, as cross-compilation is faster than compiling on a non-native platform. Signed-off-by: Yury Kulazhenkov <[email protected]>
1 parent 546b351 commit ce4b397

File tree

4 files changed

+20
-30
lines changed

4 files changed

+20
-30
lines changed

.github/workflows/image-build-test.yaml

-4
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,5 @@ jobs:
2727
with:
2828
context: .
2929
push: false
30-
# no need to explicitly set goarch,
31-
# correct arch will be selected for each build platform
32-
build-args: |
33-
goarch=
3430
platforms: ${{ env.BUILD_PLATFORMS }}
3531
file: ./cmd/Dockerfile

.github/workflows/image-push-main.yaml

-4
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,6 @@ jobs:
4545
with:
4646
context: .
4747
push: true
48-
# no need to explicitly set goarch,
49-
# correct arch will be selected for each build platform
50-
build-args: |
51-
goarch=
5248
platforms: ${{ env.BUILD_PLATFORMS }}
5349
tags: |
5450
${{ env.IMAGE_NAME }}:latest

.github/workflows/image-push-release.yaml

-4
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,6 @@ jobs:
4747
with:
4848
context: .
4949
push: true
50-
# no need to explicitly set goarch,
51-
# correct arch will be selected for each build platform
52-
build-args: |
53-
goarch=
5450
platforms: ${{ env.BUILD_PLATFORMS }}
5551
tags: |
5652
${{ steps.docker_meta.outputs.tags }}

cmd/Dockerfile

+20-18
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,37 @@
1-
FROM quay.io/centos/centos:stream9 as builder
1+
FROM --platform=$BUILDPLATFORM docker.io/golang:1.23 AS builder
22

3-
RUN mkdir /workdir
4-
WORKDIR /workdir
3+
# Support overriding target GOARCH during `make docker-build`
4+
ARG goarch=
55

6-
COPY go.mod .
6+
# these variable are automatically set during the multiarch build by docker buildx
7+
ARG TARGETOS
8+
ARG TARGETARCH
9+
ENV TARGETOS=${TARGETOS:-linux}
10+
ENV TARGETARCH=${TARGETARCH:-amd64}
711

8-
RUN dnf install -y wget
12+
ENV GOOS=${TARGETOS}
13+
ENV GOARCH=${goarch:-$TARGETARCH}
14+
ENV CGO_ENABLED=0
15+
ENV GOFLAGS=-mod=vendor
16+
17+
WORKDIR /workdir
18+
RUN mkdir /workdir/bin
919

10-
RUN GO_VERSION=$(sed -En 's/^go +(.*)$/\1/p' go.mod) && \
11-
wget https://dl.google.com/go/go${GO_VERSION}.linux-amd64.tar.gz && \
12-
tar -C /usr/local -xzf go${GO_VERSION}.linux-amd64.tar.gz && \
13-
rm go${GO_VERSION}.linux-amd64.tar.gz
20+
COPY go.mod .
21+
COPY go.sum .
1422

15-
ENV PATH /usr/local/go/bin:$PATH
23+
RUN go mod download
1624

1725
COPY . .
1826

19-
ENV GOOS linux
20-
# Support overriding target GOARCH during `make docker-build`
21-
ARG goarch=amd64
22-
ENV GOARCH=$goarch
23-
ENV CGO_ENABLED 0
24-
ENV GOFLAGS -mod=vendor
25-
26-
RUN mkdir /workdir/bin
2727
RUN go build -tags no_openssl -o /workdir/bin/ovs ./cmd/plugin
2828
RUN go build -tags no_openssl -o /workdir/bin/marker ./cmd/marker
2929
RUN go build -tags no_openssl -o /workdir/bin/ovs-mirror-producer ./cmd/mirror-producer
3030
RUN go build -tags no_openssl -o /workdir/bin/ovs-mirror-consumer ./cmd/mirror-consumer
3131

3232
FROM registry.access.redhat.com/ubi9/ubi-minimal
33+
3334
RUN microdnf install -y findutils
35+
3436
COPY --from=builder /workdir/.version /.version
3537
COPY --from=builder /workdir/bin/* /

0 commit comments

Comments
 (0)