From 58992935d35dd32da1c015133a8fb47daeb2c323 Mon Sep 17 00:00:00 2001 From: Leo Date: Wed, 5 Jun 2024 18:22:21 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E4=BC=98=E5=8C=96=20Dockerfile=20?= =?UTF-8?q?=E9=95=9C=E5=83=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit @see https://linux.do/t/topic/100676 --- Dockerfile | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/Dockerfile b/Dockerfile index f3fca4a..b242007 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,22 +1,31 @@ FROM golang:alpine AS builder WORKDIR /app -COPY . . -ENV GO111MODULE=on GOPROXY=https://goproxy.cn,direct -RUN go mod download +# 安装 ca-certificates 和 upx 包 +RUN apk --no-cache add ca-certificates upx -RUN CGO_ENABLED=0 go build -ldflags="-w -s" -o override +ENV GO111MODULE=on GOSUMDB=off GOPROXY=https://goproxy.cn -FROM alpine:latest +ADD . . -RUN apk --no-cache add ca-certificates +RUN go mod tidy -COPY --from=builder /app/override /usr/local/bin/ -COPY config.json.example /app/config.json +RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -ldflags '-s -w -extldflags "-static"' -o override && upx --best /app/override + +FROM scratch + +# 将操作系统 CA 证书复制到最终镜像 +COPY --from=builder /etc/ssl/certs /etc/ssl/certs + +# 复制静态二进制文件 +COPY --from=builder /app/override /app/override + +# 复制配置文件 +# COPY --from=builder /app/config.json /app/config.json WORKDIR /app -VOLUME /app -EXPOSE 8080 -CMD ["override"] +ENTRYPOINT ["/app/override"] + +EXPOSE 8181