|
| 1 | +# Build the manager binary |
| 2 | +FROM cr.yfdou.com/golang:latest AS builder |
| 3 | +ARG TARGETOS |
| 4 | +ARG TARGETARCH |
| 5 | + |
| 6 | +WORKDIR /workspace |
| 7 | +# Copy the Go Modules manifests |
| 8 | +COPY go.mod go.mod |
| 9 | +COPY go.sum go.sum |
| 10 | +# cache deps before building and copying source so that we don't need to re-download as much |
| 11 | +# and so that source changes don't invalidate our downloaded layer |
| 12 | +RUN go mod download |
| 13 | + |
| 14 | +# Copy the go source |
| 15 | +COPY cmd/main.go cmd/main.go |
| 16 | +COPY api/ api/ |
| 17 | +COPY internal/ internal/ |
| 18 | + |
| 19 | +# Install upx |
| 20 | +RUN sed -i "s/deb.debian.org/mirrors.aliyun.com/g" /etc/apt/sources.list.d/* \ |
| 21 | + && sed -i "s/security.debian.org/mirrors.aliyun.com/g" /etc/apt/sources.list.d/* \ |
| 22 | + && apt-get update \ |
| 23 | + && apt-get install git tar xz-utils -y \ |
| 24 | + && wget https://github.com/upx/upx/releases/download/v5.0.0/upx-5.0.0-amd64_linux.tar.xz \ |
| 25 | + && tar -xf upx-5.0.0-amd64_linux.tar.xz \ |
| 26 | + && mv upx-5.0.0-amd64_linux/upx /usr/local/bin/upx \ |
| 27 | + && rm -rf upx-5.0.0-amd64_linux* |
| 28 | + |
| 29 | +# Build |
| 30 | +# the GOARCH has not a default value to allow the binary be built according to the host where the command |
| 31 | +# was called. For example, if we call make docker-build in a local env which has the Apple Silicon M1 SO |
| 32 | +# the docker BUILDPLATFORM arg will be linux/arm64 when for Apple x86 it will be linux/amd64. Therefore, |
| 33 | +# by leaving it empty we can ensure that the container and binary shipped on it will have the same platform. |
| 34 | +RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -a -trimpath -ldflags '-w -s' -o manager cmd/main.go \ |
| 35 | + && strip --strip-unneeded manager \ |
| 36 | + && upx --lzma manager |
| 37 | + |
| 38 | +# Use distroless as minimal base image to package the manager binary |
| 39 | +# Refer to https://github.com/GoogleContainerTools/distroless for more details |
| 40 | +FROM cr.yfdou.com/gcr.io/distroless/static:nonroot |
| 41 | +WORKDIR / |
| 42 | +COPY --from=builder /workspace/manager . |
| 43 | +USER 65532:65532 |
| 44 | + |
| 45 | +ENTRYPOINT ["/manager"] |
0 commit comments