Files
upkg/example/redis-cache/Dockerfile
2026-01-28 10:28:13 +08:00

49 lines
985 B
Docker
Raw 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.
# 使用多阶段构建
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"]