build(docker): 优化Dockerfile使用多阶段构建并减少镜像大小

使用多阶段构建分离编译环境和运行环境,减少最终镜像大小
移除不必要的工具安装,仅保留运行时所需依赖
This commit is contained in:
zhouyonggao 2025-12-11 15:04:13 +08:00
parent c99d972626
commit b0e5bb0282
1 changed files with 13 additions and 8 deletions

View File

@ -1,13 +1,18 @@
FROM golang:1.21-alpine FROM golang:1.21-alpine AS builder
ENV CGO_ENABLED=0 ENV CGO_ENABLED=0
ENV GO111MODULE=on
ENV GOPROXY=https://goproxy.cn,direct ENV GOPROXY=https://goproxy.cn,direct
RUN apk add --no-cache git tzdata
# 安装必要的工具 WORKDIR /app
RUN apk add --no-cache git tzdata bash curl COPY server/go.mod server/go.sum ./server/
WORKDIR /app/server WORKDIR /app/server
RUN go mod download
COPY server/ ./
RUN go build -o /out/server ./cmd/server/main.go
FROM alpine:3.19
RUN apk add --no-cache tzdata ca-certificates curl bash
WORKDIR /app
COPY --from=builder /out/server /app/server
EXPOSE 8077 EXPOSE 8077
ENTRYPOINT ["/app/server"]
CMD ["go", "run", "cmd/server/main.go"]