# 多阶段构建,减小最终镜像体积 FROM golang:1.22-alpine AS builder WORKDIR /src # 先复制 go.mod / go.sum 并下载依赖,利用 Docker 缓存 COPY go.mod go.sum* ./ RUN go mod download COPY . . # 静态二进制,方便在 alpine 中运行 RUN CGO_ENABLED=0 GOOS=linux go build -trimpath -ldflags="-s -w" -o /out/auth-service ./cmd/auth-service FROM alpine:3.19 RUN apk --no-cache add ca-certificates tzdata && adduser -D -u 10001 app WORKDIR /app COPY --from=builder /out/auth-service /app/auth-service USER app EXPOSE 8081 ENTRYPOINT ["/app/auth-service"]