1+ # syntax=docker/dockerfile:1.1-experimental
2+
13# Copyright 2019 The Kubernetes Authors.
24#
35# Licensed under the Apache License, Version 2.0 (the "License");
1315# limitations under the License.
1416
1517# Build the manager binary
16- FROM golang:1.13.15 as builder
18+ FROM golang:1.15.3 as builder
1719WORKDIR /workspace
1820
1921# Run this with docker build --build_arg $(go env GOPROXY) to override the goproxy
@@ -23,20 +25,33 @@ ENV GOPROXY=$goproxy
2325# Copy the Go Modules manifests
2426COPY go.mod go.mod
2527COPY go.sum go.sum
28+
2629# Cache deps before building and copying source so that we don't need to re-download as much
2730# and so that source changes don't invalidate our downloaded layer
28- RUN go mod download
31+ RUN --mount=type=cache,target=/go/pkg/mod \
32+ go mod download
2933
3034# Copy the sources
3135COPY ./ ./
3236
37+ # Cache the go build into the the Go’s compiler cache folder so we take benefits of compiler caching across docker build calls
38+ RUN --mount=type=cache,target=/root/.cache/go-build \
39+ --mount=type=cache,target=/go/pkg/mod \
40+ go build .
41+
3342# Build
43+ ARG package=.
3444ARG ARCH
35- RUN CGO_ENABLED=0 GOOS=linux GOARCH=${ARCH} \
36- go build -a -ldflags '-extldflags "-static"' \
37- -o manager .
45+ ARG ldflags
46+
47+ # Do not force rebuild of up-to-date packages (do not use -a) and use the compiler cache folder
48+ RUN --mount=type=cache,target=/root/.cache/go-build \
49+ --mount=type=cache,target=/go/pkg/mod \
50+ CGO_ENABLED=0 GOOS=linux GOARCH=${ARCH} \
51+ go build -ldflags "${ldflags} -extldflags '-static'" \
52+ -o manager ${package}
3853
39- # Copy the controller-manager into a thin image
54+ # Production image
4055FROM gcr.io/distroless/static:latest
4156WORKDIR /
4257COPY --from=builder /workspace/manager .
0 commit comments