Files
SiteProxy/Dockerfile
2025-12-15 02:47:12 +08:00

52 lines
1.0 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
FROM golang:1.25-alpine AS builder
WORKDIR /app
# 安装依赖
RUN apk add --no-cache git
# 复制 go 模块文件
COPY go.mod ./
# 如果存在 go.sum 则复制
COPY go.su[m] ./
# 下载依赖
RUN go mod download
# 复制源代码
COPY . .
# 生成 go.sum如果不存在
RUN go mod tidy
# 编译
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -ldflags="-s -w" -o siteproxy .
# 运行阶段
FROM alpine:latest
RUN apk --no-cache add ca-certificates tzdata
WORKDIR /app
# 从构建阶段复制二进制文件和模板
COPY --from=builder /app/siteproxy .
COPY --from=builder /app/templates ./templates
# 创建非 root 用户
RUN addgroup -g 1000 appuser && \
adduser -D -u 1000 -G appuser appuser && \
chown -R appuser:appuser /app
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 ["./siteproxy"]