Skip to content

RAS-1976: Fix cross-building Docker images - #180

Merged
warrenbaileyons merged 3 commits into
mainfrom
fix-arm64-cross-compile
Oct 1, 2026
Merged

warrenbaileyons merged 3 commits into
mainfrom
fix-arm64-cross-compile

Conversation

@prem-buczkowski-ons

Copy link
Copy Markdown
Contributor

What and why?

The final stage was pinned to --platform=$BUILDPLATFORM and go build never set GOARCH, so build of the arm64 image in CI/CD built for amd64 and labelled it arm64 nevertheless - and it failed to run with an "exec format error". Add ARG TARGETARCH + GOOS/GOARCH, unpin the final stage.

(If you built on a Mac, then you'd get arm64 for both instead, so the code seemed to work.)

Nicely, it means that the build stage will cross-compile on the native compiler without emulation, so the build stage for both arm64 and amd64 should take roughly the same time, no matter the architecture of the build machine.

Also swap the runtime image to alpine:3.24 (~250MB -> ~10MB), adding the ca-certificates and CGO_ENABLED=0 that were implicit in golang:*-alpine.

How to test?

# architecture check of the main binary in the image
docker buildx build --platform linux/arm64 --output type=tar,dest=- \
  -f _infra/docker/Dockerfile . \
  | tar -xOf - opt/survey/main \
  | { dd bs=1 skip=18 count=2 2>/dev/null; cat >/dev/null; } \
  | od -An -tx1

# the same, but for amd64
docker buildx build --platform linux/amd64 --output type=tar,dest=- \
  -f _infra/docker/Dockerfile . \
  | tar -xOf - opt/survey/main \
  | { dd bs=1 skip=18 count=2 2>/dev/null; cat >/dev/null; } \
  | od -An -tx1

It should say b7 00 for arm64, and 3e 00 for x86-64 (as it did for both before).

Jira

https://officefornationalstatistics.atlassian.net/browse/RAS-1976

The final stage was pinned to --platform=$BUILDPLATFORM and go build never
set GOARCH, so every pass built for amd64 and BuildKit labelled it arm64.
Add ARG TARGETARCH + GOOS/GOARCH, unpin the final stage.

Also swap the runtime image to alpine:3.24 (~250MB -> ~10MB), adding the
ca-certificates and CGO_ENABLED=0 that golang:*-alpine gave us implicitly.
@arroyoAle

Copy link
Copy Markdown
Contributor

/gcbrun

@warrenbaileyons
warrenbaileyons merged commit 415fd43 into main Oct 1, 2026
6 checks passed
@warrenbaileyons
warrenbaileyons deleted the fix-arm64-cross-compile branch October 1, 2026 12:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants