Skip to content

Commit 945fd8e

Browse files
stubbiclaude
andcommitted
fix: optimize Dockerfile for multi-arch builds without QEMU
Use BUILDPLATFORM for the builder stage so Go compiles natively. When GoReleaser provides PREBUILT_BINARY, copy the pre-compiled native binary from the platform-specific directory instead of cross-compiling inside Docker with QEMU emulation. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 4435431 commit 945fd8e

1 file changed

Lines changed: 15 additions & 17 deletions

File tree

Dockerfile

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,29 @@
11
# Build the manager binary
2-
FROM golang:1.24-alpine AS builder
2+
FROM --platform=${BUILDPLATFORM:-linux/amd64} golang:1.24-alpine AS builder
33
ARG TARGETOS
44
ARG TARGETARCH
5+
ARG TARGETPLATFORM
56
ARG PREBUILT_BINARY
67

78
WORKDIR /workspace
8-
# Copy the Go Modules manifests
9-
COPY go.mod go.mod
10-
COPY go.sum go.sum
11-
# cache deps before building and copying source so that we don't need to re-download as much
12-
# and so that source changes don't invalidate our downloaded layer
13-
RUN go mod download
149

15-
# Copy the go source
16-
COPY cmd/main.go cmd/main.go
17-
COPY api/ api/
18-
COPY internal/ internal/
10+
# Copy dependency files first for caching
11+
COPY go.mod go.sum ./
12+
RUN if [ -z "$PREBUILT_BINARY" ]; then go mod download; fi
1913

20-
# Build - use prebuilt binary from GoReleaser if available
21-
RUN if [ -n "$PREBUILT_BINARY" ] && [ -f "$PREBUILT_BINARY" ]; then \
22-
cp "$PREBUILT_BINARY" manager; \
14+
# Copy everything else (source code, and GoReleaser platform dirs if present)
15+
COPY . .
16+
17+
# Use pre-built binary if available (GoReleaser places them in platform dirs),
18+
# otherwise build from source. When PREBUILT_BINARY is set, GoReleaser has
19+
# already cross-compiled native binaries -- no QEMU needed.
20+
RUN if [ -n "$PREBUILT_BINARY" ] && [ -f "${TARGETPLATFORM}/${PREBUILT_BINARY}" ]; then \
21+
cp "${TARGETPLATFORM}/${PREBUILT_BINARY}" manager; \
2322
else \
24-
CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -a -o manager cmd/main.go; \
23+
CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH:-amd64} go build -a -o manager cmd/main.go; \
2524
fi
2625

27-
# Use distroless as minimal base image to package the manager binary
28-
# Refer to https://github.com/GoogleContainerTools/distroless for more details
26+
# Runtime stage - use distroless for minimal attack surface
2927
FROM gcr.io/distroless/static:nonroot
3028
WORKDIR /
3129
COPY --from=builder /workspace/manager .

0 commit comments

Comments
 (0)