HydraIssues

Cross-compile the Go container builds instead of emulating arm64 under QEMU
open improvement Priority: medium Project: hydracluster Reporter: 13 Nov 2026 00:35

Description

Every multi-arch hydra image builds its arm64 half by running the whole Go toolchain inside an emulated arm64 container. It works, it is slow, and now that CI has moved onto our own runners the cost is wall-clock and disk on a box we own rather than billed GitHub minutes. nimsforest already builds the right way, so this is adopting an in-house pattern rather than inventing one.

What is actually there

I surveyed every repo with a local checkout on neoremote: 25 hydra repos push a multi-arch image (platforms: linux/amd64,linux/arm64) and none of them cross-compiles. Not one Dockerfile mentions TARGETARCH or BUILDPLATFORM. They all open with a bare

FROM golang:1.25-alpine AS build

so under buildx the arm64 leg runs apk add git, go mod download and go build inside QEMU user-mode emulation. The amd64 leg runs native. The arm64 leg does the same work several times slower.

The repos: hydraapplepipeline, hydrabiamp, hydrabodystatus, hydrabooks, hydrabrainserver, hydradistrict, hydraepiphan, hydraexperiencelibrary, hydragitwatcher, hydraissue, hydramancer, hydramirror, hydranorthstar, hydranps, hydraorganization, hydraperforce, hydrapipeline, hydraroar, hydrastreamingmonitor, hydratam, hydratransfer, hydraunrealengine-server, hydravenues, hydrawebsite. That is 24.

hydraadb is the 25th and is out of scope: it is FROM debian:bookworm-slim, not a Go build, so it needs its own look and should not be swept into this change.

The precedent is already in the estate

nimsforest2, nimsforestadmin and nimsforestwebchat all cross-compile today. nimsforest2/Dockerfile:

FROM --platform=$BUILDPLATFORM golang:1.25.7-alpine AS builder
...
ARG TARGETOS
ARG TARGETARCH
RUN CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETARCH go build -ldflags "-s -w -X main.version=${VERSION}" -o /forest ./cmd/forest

That is the whole trick. --platform=$BUILDPLATFORM pins the build stage to the runner's native architecture, and GOARCH tells Go what to emit. Go cross-compiles natively, so the arm64 binary is produced by a native amd64 toolchain at native speed.

Why it is safe here

All 24 already set CGO_ENABLED=0. That is the condition that makes this trivial: with cgo off there is no C toolchain to cross-target and no sysroot to install, so the change is three lines per Dockerfile and nothing else moves. The output is byte-for-byte the same static binary the emulated build produces, reached by a different route.

The runtime stage still runs emulated, because FROM alpine:3.21 + RUN apk add --no-cache ca-certificates executes arm64 package tooling. That is a handful of seconds against a full module download and compile, and leaving it alone keeps the change small. Worth revisiting only if apk turns out to be a flake source.

The change, per repo

  1. FROM golang:1.25-alpine AS build becomes FROM --platform=$BUILDPLATFORM golang:1.25-alpine AS build.
  2. Declare ARG TARGETOS and ARG TARGETARCH in the build stage, next to the existing ARG VERSION.
  3. The build line gains GOOS=$TARGETOS GOARCH=$TARGETARCH, keeping CGO_ENABLED=0 and the existing -trimpath and -ldflags.

No workflow change. platforms: stays as it is; buildx already passes TARGETOS and TARGETARCH, they are simply not being read.

Verification, per repo

  • Tag, then docker buildx imagetools inspect <registry>/<name>:<tag> and confirm both linux/amd64 and linux/arm64 are present. Most of these workflows already assert this and will catch a regression themselves.
  • Pull the arm64 image on a pi node and run the binary, because a manifest entry only proves something was pushed under that platform, not that it executes. A wrong GOARCH yields a manifest that looks right and a container that will not start.
  • Compare job duration against the previous tag. If the arm64 leg has not got materially faster, the ARGs are not being read and the change is a no-op dressed as a fix.

Order

One repo first, all the way through to a running container on a pi node, then the rest. hydrabodystatus or hydratam is the right pilot: small, already exercised on the org runner, and recently released so a comparison is available. Do not batch 24 Dockerfile edits before one of them has been proven on real hardware.

Caveat

The survey covers repos checked out on neoremote. A hydra repo with no local checkout will not appear in the list above and should be swept separately.

Where this came from

Noticed while reviewing hydravenues' move to a self-hosted org runner. On hosted runners the emulated leg was billed minutes and invisible; on our own boxes it is wall-clock and build cache on a shared machine that is already at 69% disk. Raised there, and deliberately kept out of the migration so the move changed one variable at a time. Background on the runner move: hydracluster docs/runbooks/github-org-migration.md, commit 736d0f7.