AnCao/Dockerfile
yanlongqi 53d3ebe318 feat: 优化静态文件服务和部署配置
主要改进:
- 重构静态文件服务: 实现自定义 StaticFileHandler,完善 SPA 路由支持和 Content-Type 处理
- 优化 Docker 构建: 简化前端资源复制逻辑,直接复制整个 dist 目录
- 添加 K8s 部署配置: 包含健康检查探针、资源限制和服务配置
- 前端配置优化: Vite 使用相对路径 base,确保打包后资源路径正确
- 代码清理: 删除已废弃的数据迁移脚本

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-10 13:12:20 +08:00

72 lines
1.6 KiB
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 node:22-alpine AS frontend-builder
WORKDIR /app/web
# 复制前端依赖文件
COPY web/package.json web/yarn.lock ./
# 安装依赖
RUN yarn install --frozen-lockfile
# 复制前端源代码
COPY web/ ./
# 构建前端应用
RUN yarn build
# ============================================
# 第二阶段: 构建 Go 后端应用
# ============================================
FROM golang:1.25.1-alpine AS backend-builder
WORKDIR /app
# 安装必要的构建工具
RUN apk add --no-cache git
# 配置 Go 代理(使用国内镜像加速)
ENV GOPROXY=https://goproxy.cn,https://goproxy.io,https://mirrors.aliyun.com/goproxy/,direct
ENV GOSUMDB=off
# 复制 Go 依赖文件
COPY go.mod go.sum ./
# 下载依赖
RUN go mod download
# 复制后端源代码
COPY . .
# 构建 Go 应用
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o server main.go
# ============================================
# 第三阶段: 创建最终运行镜像
# ============================================
FROM alpine:latest
WORKDIR /app
# 安装运行时依赖
RUN apk --no-cache add ca-certificates tzdata
# 设置时区为上海
ENV TZ=Asia/Shanghai
# 从后端构建阶段复制可执行文件
COPY --from=backend-builder /app/server ./server
# 从前端构建阶段复制构建产物
# 直接将整个 dist 目录复制为 web 目录
# dist 目录包含index.html, assets/, icon.svg 等所有构建产物
COPY --from=frontend-builder /app/web/dist ./web
# 暴露端口
EXPOSE 8080
# 启动应用
CMD ["./server"]