镜像多阶段构建
FROM golang:1.17 as builder
RUN apt-get -y update && apt-get -y install upx
WORKDIR /workspace
COPY go.mod go.mod
COPY go.sum go.sum
COPY webhook/ webhook/
COPY tls/ tls/
COPY pkg/ pkg/
ENV CGO_ENABLED=0
ENV GOOS=linux
ENV GOARCH=amd64
ENV GO111MODULE=on
ENV GOPROXY="https://goproxy.cn"
RUN go mod download && \
go build -a -o admission-registry ./webhook && \
go build -a -o tls-manager ./tls && \
upx admission-registry tls-manager
# upx 是压缩 这个不用解压 运行时,系统会自动解压运行
FROM alpine:3.9.2 as manager
COPY --from=builder /workspace/admission-registry .
ENTRYPOINT ["/admission-registry"]
# docker build --target manager -t dfy007/admission-webhook-example:v1.9 -f Dockerfile .
FROM alpine:3.9.2 as tls
COPY --from=builder /workspace/tls-manager .
ENTRYPOINT ["/tls-manager"]
# docker build --target tls -t dfy007/admission-registry-tls:v1.9 -f Dockerfile .
指定某个阶段构建进行
# 采用 --target 参数
# --target 指定,只执行上面的最后阶段
# -t 指定构建后的镜像名称
# -f 指定采用哪个 Dockerfile
docker build --target tls -t dfy007/admission-registry-tls:v1.9 -f Dockerfile .