Skip to content

Commit 3e2c271

Browse files
committed
scaffold: adjust Dockerfile template to support multi-arch builds
1 parent da5c02c commit 3e2c271

File tree

1 file changed

+9
-11
lines changed

1 file changed

+9
-11
lines changed

scaffold/templates/Dockerfile.tpl

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,33 @@
11
# Build the manager binary
2-
FROM golang:{{ .goVersion }} as builder
2+
FROM --platform=$BUILDPLATFORM golang:{{ .goVersion }} as builder
33
ARG TARGETOS
44
ARG TARGETARCH
55

66
WORKDIR /workspace
77
# Copy the Go Modules manifests
88
COPY go.mod go.mod
99
COPY go.sum go.sum
10-
# cache deps before building and copying source so that we don't need to re-download as much
10+
# Cache deps before building and copying source so that we don't need to re-download as much
1111
# and so that source changes don't invalidate our downloaded layer
1212
RUN go mod download
1313

14-
# Copy the go source
14+
# Copy the go sources
1515
COPY main.go main.go
1616
COPY api/ api/
1717
COPY pkg/ pkg/
18-
COPY hack/ hack/
18+
COPY crds/ crds/
1919
COPY Makefile Makefile
2020

21-
# Build
22-
# the GOARCH has not a default value to allow the binary be built according to the host where the command
23-
# was called. For example, if we call make docker-build in a local env which has the Apple Silicon M1 SO
24-
# the docker BUILDPLATFORM arg will be linux/arm64 when for Apple x86 it will be linux/amd64. Therefore,
25-
# by leaving it empty we can ensure that the container and binary shipped on it will have the same platform.
26-
RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} make test build
21+
# Run tests and build
22+
RUN make envtest \
23+
&& CGO_ENABLED=0 KUBEBUILDER_ASSETS="/workspace/bin/k8s/current" go test ./... \
24+
&& CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -a -o manager main.go
2725

2826
# Use distroless as minimal base image to package the manager binary
2927
# Refer to https://github.com/GoogleContainerTools/distroless for more details
3028
FROM gcr.io/distroless/static:nonroot
3129
WORKDIR /
32-
COPY --from=builder /workspace/bin/manager .
30+
COPY --from=builder /workspace/manager .
3331
USER 65532:65532
3432

3533
ENTRYPOINT ["/manager"]

0 commit comments

Comments
 (0)