Files
perler-lab/perler-lab/Dockerfile
T

55 lines
2.4 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.
# syntax=docker/dockerfile:1
# =============================================================================
# PERLER LAB — 前端 Dockerfile(多阶段构建)
# 构建阶段:node:20-alpine + Vite 构建 React 应用 → dist/
# 运行阶段:nginx:alpine 提供静态资源服务(SPA 路由回退由 nginx.conf 处理)
# =============================================================================
# ── 第一阶段:构建 ──
FROM node:20-alpine AS build
WORKDIR /app
# 复制依赖清单(有 package-lock.json,使用 npm ci 保证可复现)
COPY package.json package-lock.json ./
RUN npm ci --no-audit --no-fund
# 复制 Vite/TS 配置与 HTML 入口
COPY tsconfig.json vite.config.ts index.html ./
# 复制源码与静态资源(public/ 由 Vite 在构建时合并进 dist/)
COPY src ./src
COPY public ./public
# 生产 API 基址:设为 "/",这样 request() 拼出的路径如 "//api/auth/login"
# 浏览器自动规范化为 "/api/auth/login",再由 Nginx "/api/" 位置反代到后端 :8788。
# 若设为空字符串则路径无变化,但 "/" 更语义明确。
# 注意:仅影响本镜像构建;本地 `npm run dev`(Vite 5173)无此变量,仍回退到
# src/lib/api.ts 中的开发默认值 http://localhost:8788。
ARG VITE_API_BASE=/
ENV VITE_API_BASE=$VITE_API_BASE
# 构建 React 应用 → dist/(与 package.json 的 build 脚本一致:vite build)
RUN npm run build
# ── 第二阶段:Nginx 运行 ──
FROM nginx:1.27-alpine
# 验证码平台 upstream 默认值(公网 IP,测试环境/外部部署直接可用);
# 生产 deploy-prod.yml 用 -e CAPTCHA_UPSTREAM=10.9.0.2:18200 覆盖为 WireGuard 内网
ENV CAPTCHA_UPSTREAM=38.76.218.110:18200
# 以模板方式挂载:nginx 镜像入口启动时用 envsubst 渲染 ${CAPTCHA_UPSTREAM} 后
# 生成 /etc/nginx/conf.d/default.conf(官方镜像 /docker-entrypoint.d/20-envsubst-on-templates.sh)
COPY nginx.conf /etc/nginx/templates/default.conf.template
# 复制构建产物(dist/ 含 index.html + assets/)到 nginx 根目录
COPY --from=build /app/dist /usr/share/nginx/html
# 健康检查——nginx 仅监听 IPv4 (0.0.0.0:80),必须用 127.0.0.1 而非 localhost
# (localhost 会解析到 ::1 IPv6,导致 wget 连接被拒)
HEALTHCHECK --interval=30s --timeout=5s --retries=3 --start-period=10s \
CMD wget -qO- http://127.0.0.1:80/healthz || exit 1
EXPOSE 80