refactor: 优化 Dockerfile 镜像

@see https://linux.do/t/topic/100676
This commit is contained in:
Leo 2024-06-05 18:22:21 +08:00
parent 19447a898a
commit 58992935d3
1 changed files with 20 additions and 11 deletions

View File

@ -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