Files

35 lines
1.3 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.
# ---------- 第一阶段:构建 ----------
FROM node:20-alpine AS build
WORKDIR /app
# 构建阶段必须是开发环境,否则 npm 会跳过 devDependenciestsc/vite 缺失 → 127
ENV NODE_ENV=development
# 先只拷贝依赖清单,利用 Docker 层缓存加速重复构建
COPY package.json package-lock.json* ./
# 国内网络环境可取消下一行注释使用镜像源
# RUN npm config set registry https://registry.npmmirror.com
RUN npm ci --include=dev
# 兜底:某些环境的 npm 配置会强制跳过 devDependencies(导致 tsc 缺失、127 报错)
# 显式按 package.json 声明逐个安装,explicit install 不受 omit 配置影响
RUN npm install --no-save --force $(node -e "console.log(Object.keys(require('./package.json').devDependencies||{}).join(' '))")
# 依赖自检:如果 tsc/vite 没装上,在这里就明确失败而不是 127
RUN ./node_modules/.bin/tsc --version && ./node_modules/.bin/vite --version
# 拷贝源码并构建(产物输出到 /app/dist)
COPY . .
RUN npm run build
# ---------- 第二阶段:运行 ----------
FROM nginx:1.27-alpine
# 单页应用 Nginx 配置(SPA 回退 + 静态资源缓存)
COPY nginx.conf /etc/nginx/conf.d/default.conf
# 拷贝构建产物
COPY --from=build /app/dist /usr/share/nginx/html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]