sigs.k8s.io/kubebuilder/v3@v3.14.0/pkg/plugins/golang/v3/scaffolds/internal/templates/dockerfile.go (about) 1 /* 2 Copyright 2020 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package templates 18 19 import ( 20 "sigs.k8s.io/kubebuilder/v3/pkg/machinery" 21 ) 22 23 var _ machinery.Template = &Dockerfile{} 24 25 // Dockerfile scaffolds a file that defines the containerized build process 26 type Dockerfile struct { 27 machinery.TemplateMixin 28 } 29 30 // SetTemplateDefaults implements file.Template 31 func (f *Dockerfile) SetTemplateDefaults() error { 32 if f.Path == "" { 33 f.Path = "Dockerfile" 34 } 35 36 f.TemplateBody = dockerfileTemplate 37 38 return nil 39 } 40 41 const dockerfileTemplate = `# Build the manager binary 42 FROM golang:1.19 as builder 43 ARG TARGETOS 44 ARG TARGETARCH 45 46 WORKDIR /workspace 47 # Copy the Go Modules manifests 48 COPY go.mod go.mod 49 COPY go.sum go.sum 50 # cache deps before building and copying source so that we don't need to re-download as much 51 # and so that source changes don't invalidate our downloaded layer 52 RUN go mod download 53 54 # Copy the go source 55 COPY main.go main.go 56 COPY api/ api/ 57 COPY controllers/ controllers/ 58 59 # Build 60 # the GOARCH has not a default value to allow the binary be built according to the host where the command 61 # was called. For example, if we call make docker-build in a local env which has the Apple Silicon M1 SO 62 # the docker BUILDPLATFORM arg will be linux/arm64 when for Apple x86 it will be linux/amd64. Therefore, 63 # by leaving it empty we can ensure that the container and binary shipped on it will have the same platform. 64 RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -a -o manager main.go 65 66 # Use distroless as minimal base image to package the manager binary 67 # Refer to https://github.com/GoogleContainerTools/distroless for more details 68 FROM gcr.io/distroless/static:nonroot 69 WORKDIR / 70 COPY --from=builder /workspace/manager . 71 USER 65532:65532 72 73 ENTRYPOINT ["/manager"] 74 `