|
1 | 1 | # Build the manager binary |
2 | | -FROM golang:1.24-alpine AS builder |
| 2 | +FROM --platform=${BUILDPLATFORM:-linux/amd64} golang:1.24-alpine AS builder |
3 | 3 | ARG TARGETOS |
4 | 4 | ARG TARGETARCH |
| 5 | +ARG TARGETPLATFORM |
5 | 6 | ARG PREBUILT_BINARY |
6 | 7 |
|
7 | 8 | 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 |
14 | 9 |
|
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 |
19 | 13 |
|
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; \ |
23 | 22 | 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; \ |
25 | 24 | fi |
26 | 25 |
|
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 |
29 | 27 | FROM gcr.io/distroless/static:nonroot |
30 | 28 | WORKDIR / |
31 | 29 | COPY --from=builder /workspace/manager . |
|
0 commit comments