Files
goalfylearning-admin/Dockerfile

62 lines
1.3 KiB
Docker
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 多阶段构建 Dockerfile
# 阶段1构建阶段
FROM golang:1.25-alpine AS builder
# 设置工作目录
WORKDIR /app
# 安装必要的构建工具
RUN apk add --no-cache git
# 复制整个项目
COPY . .
# 生成 go.sum 并下载依赖
RUN go mod tidy && go mod download
# 构建应用
# 使用静态编译,减小镜像体积
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build \
-ldflags="-w -s -X 'main.env=prod'" \
-o goalfymax-admin \
./cmd/server
# 阶段2运行阶段
FROM alpine:latest
# 安装必要的运行时依赖
RUN apk --no-cache add ca-certificates tzdata
# 设置时区为 UTC
ENV TZ=UTC
# 创建非 root 用户
RUN addgroup -g 1000 appuser && \
adduser -D -u 1000 -G appuser appuser
# 设置工作目录
WORKDIR /app
# 从构建阶段复制二进制文件
COPY --from=builder /app/goalfymax-admin .
# 创建配置文件目录(配置文件将通过 ConfigMap 挂载)
RUN mkdir -p /app/etc
# 修改文件所有者
RUN chown -R appuser:appuser /app
# 切换到非 root 用户
USER appuser
# 暴露应用端口
EXPOSE 8087
# 健康检查
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
CMD wget --no-verbose --tries=1 --spider http://localhost:8087/health || exit 1
# 启动应用
ENTRYPOINT ["/app/goalfymax-admin"]
CMD ["--env", "prod"]