Files
chat/frontend/Dockerfile.prod
T
2026-06-15 21:58:25 +08:00

47 lines
1.9 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.
# ============================================================
# 青叶 —— 生产环境前端镜像(多阶段构建)
# Stage 1: Node 构建 Vite 生产包
# Stage 2: Nginx 提供静态文件 + SPA 路由回退
# ------------------------------------------------------------
# 构建「域名无关」:不设置任何 VITE_* 变量。前端代码在没有 VITE_API_BASE_URL /
# VITE_WS_BASE_URL 时,会自动使用页面同源地址(API 走相对路径 /api/v1
# WebSocket 走 wss://<当前域名>)。因此同一份镜像可部署到任意域名。
# ============================================================
# ===== Stage 1: 构建 =====
FROM node:20-alpine AS builder
WORKDIR /app
# 复制依赖描述(package-lock.json 可选 —— 仓库未提交锁文件,用 npm install
COPY package.json package-lock.json* ./
# 安装依赖(使用 npm 官方源)
RUN npm install
# 复制源码(frontend/.dockerignore 已排除 node_modules / dist / .env 等)
COPY . .
# Vite 打包(含 echarts / vue-echarts / naive-ui 等大依赖)较吃内存。
# 显式调高 V8 堆上限,避免在内存受限的 VPS 上报 "JavaScript heap out of memory"。
# 此值只是「允许上限」,V8 仅按需占用;若 VPS 物理内存很小,仍需加 swap(见 DEPLOYMENT.md)。
ENV NODE_OPTIONS="--max-old-space-size=2048"
# 直接调用 vite build,跳过 vue-tsc 类型检查
# (避免仓库中既有的类型错误阻断生产部署;类型检查请在 CI / 发布前单独执行)
# 如需严格的类型门禁,可改为 `npm run build`。
RUN npx vite build
# ===== Stage 2: 运行(Nginx 静态服务) =====
FROM nginx:alpine
# 复制构建产物
COPY --from=builder /app/dist /usr/share/nginx/html
# 复制 SPA 专用 Nginx 配置(createWebHistory 需要 try_files 回退)
COPY nginx.conf /etc/nginx/conf.d/default.conf
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]