Files
web_template/Dockerfile
2026-07-10 10:05:52 +00:00

60 lines
1.4 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.
# ============================================
# Multi-stage Dockerfile for gotest
# Stage 1: Build (Go 1.25)
# Stage 2: Runtime (minimal Alpine)
# ============================================
# ---- Build Stage ----
FROM golang:1.25-alpine AS builder
# 安装必要的构建工具
RUN apk add --no-cache git ca-certificates tzdata
WORKDIR /build
# 利用 Docker 层缓存:先拷贝依赖文件,再下载依赖
COPY go.mod go.sum ./
RUN go mod download
# 拷贝源代码
COPY . .
# 构建:CGO_ENABLED=0 纯静态二进制,适合 scratch/alpine
# 前端模板通过 //go:embed 已嵌入二进制
ENV CGO_ENABLED=0 GOOS=linux GOARCH=amd64
RUN go build -ldflags="-s -w" -o gotest .
# ---- Runtime Stage ----
FROM alpine:3.20
# 安装最小运行时依赖
RUN apk add --no-cache ca-certificates tzdata
# 设置时区
ENV TZ=Asia/Shanghai
# 创建非 root 用户运行
RUN addgroup -S app && adduser -S app -G app
# 创建数据和上传目录
RUN mkdir -p /app/data /app/uploads && chown -R app:app /app
WORKDIR /app
# 从构建阶段拷贝二进制和配置
COPY --from=builder /build/gotest .
COPY --from=builder /build/config.yaml .
# 切换非 root 用户
USER app
# 暴露端口
EXPOSE 8080
# 健康检查
HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \
CMD wget -q -O- http://localhost:8080/api/health || exit 1
# 启动
ENTRYPOINT ["./gotest"]