This commit is contained in:
loveuer
2026-01-28 10:28:13 +08:00
parent 507a67e455
commit 3ee0c9c098
29 changed files with 2852 additions and 0 deletions

View File

@@ -0,0 +1,49 @@
# 使用多阶段构建
FROM golang:1.25-alpine AS builder
# 安装 git
RUN apk add --no-cache git
# 设置工作目录
WORKDIR /app
# 复制整个项目(包括父级目录)
COPY ../../. /upkg
COPY . .
# 下载依赖
RUN go mod download
# 构建应用
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main .
# 使用轻量级镜像
FROM alpine:latest
# 安装 ca-certificates用于 HTTPS 请求)
RUN apk --no-cache add ca-certificates
# 创建非 root 用户
RUN addgroup -g 1001 -S appgroup && \
adduser -u 1001 -S appuser -G appgroup
WORKDIR /root/
# 从构建阶段复制二进制文件
COPY --from=builder /app/main .
# 修改文件所有者
RUN chown appuser:appgroup main
# 切换到非 root 用户
USER appuser
# 暴露端口
EXPOSE 8080
# 健康检查
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
CMD wget --no-verbose --tries=1 --spider http://localhost:8080/health || exit 1
# 启动应用
CMD ["./main"]