From 4bbcdac5f0c4daa032551950b30fd0f9d80734d0 Mon Sep 17 00:00:00 2001 From: Yuki Iwai Date: Fri, 16 Aug 2024 02:49:32 +0900 Subject: [PATCH] KEP-2170: Bind repository into the build environment instead of filecopy Signed-off-by: Yuki Iwai --- cmd/training-operator.v2alpha1/Dockerfile | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/cmd/training-operator.v2alpha1/Dockerfile b/cmd/training-operator.v2alpha1/Dockerfile index 6c98c27964..a7515812cd 100644 --- a/cmd/training-operator.v2alpha1/Dockerfile +++ b/cmd/training-operator.v2alpha1/Dockerfile @@ -1,23 +1,20 @@ # Build the manager binary -FROM golang:1.22 as builder +FROM golang:1.22 AS builder WORKDIR /workspace -# Copy the Go Modules manifests -COPY go.mod go.mod -COPY go.sum go.sum -# cache deps before building and copying source so that we don't need to re-download as much -# and so that source changes don't invalidate our downloaded layer -RUN go mod download - -# Copy the go source -COPY . . # Build -RUN CGO_ENABLED=0 GOOS=linux GO111MODULE=on go build -a -o manager cmd/training-operator.v2alpha1/main.go +RUN --mount=type=cache,target=/go/pkg/mod/,sharing=locked \ + --mount=type=bind,source=go.sum,target=go.sum \ + --mount=type=bind,source=go.mod,target=go.mod \ + go mod download +RUN --mount=type=cache,target=/go/pkg/mod/ \ + --mount=type=bind,target=. \ + CGO_ENABLED=0 GOOS=linux GO111MODULE=on go build -a -o /bin/manager cmd/training-operator.v2alpha1/main.go # Use distroless as minimal base image to package the manager binary # Refer to https://github.com/GoogleContainerTools/distroless for more details FROM gcr.io/distroless/static:nonroot WORKDIR / -COPY --from=builder /workspace/manager . +COPY --from=builder /bin/manager . ENTRYPOINT ["/manager"]