This commit is contained in:
2026-02-03 19:37:58 +08:00
parent 8ffe3e0e15
commit db4a0b109c
42 changed files with 6594 additions and 183 deletions

View File

@@ -1,41 +1,44 @@
# ===============================
# 1) 依赖安装 + 构建阶段
# ===============================
FROM node:16-alpine AS build
FROM node:20.15.0-alpine AS builder
# 设定工作目录,后续所有指令基于此,避免路径混乱
WORKDIR /app
# 只拷贝 package.json加快缓存命中
# 配置npm国内源仅1次配置后续所有npm命令自动复用删除冗余的registry传参
ENV NPM_REGISTRY=https://registry.npmmirror.com/
RUN npm config set registry ${NPM_REGISTRY}
# 先拷贝package.json核心利用Docker层缓存代码不变则不重装依赖
COPY package.json ./
# 预先安装依赖(利用缓存
RUN npm install --registry=https://registry.npmmirror.com
# 安装依赖(无需重复写registry已全局配置加快构建
RUN npm install
# 拷贝全部代码
# 拷贝全部项目代码(放在装依赖后,最大化缓存收益)
COPY . .
# 通过构建参数传入构建环境(如 sit/test/prod
# 构建参数默认sitbuild时可通过--build-arg覆盖如prod/test
ARG BUILD_ENV=sit
# 注入环境变量,确保构建脚本能读取到(部分框架需要环境变量透传)
ENV BUILD_ENV=${BUILD_ENV}
# 要求 package.json 中的 script 形如:build:sit, build:test, build:prod
# 执行环境构建命令(兼容package.jsonbuild:sit/build:test/build:prod
RUN npm run build:${BUILD_ENV}
# ===============================
# 2) 生产镜像阶段
# 2) 生产镜像阶段轻量Nginx仅保留构建产物
# ===============================
FROM nginx:1.25-alpine AS runtime
# 清理默认 nginx 静态内容
RUN rm -rf /usr/share/nginx/html/*
# 拷贝 build 结果
COPY --from=build /app/dist /usr/share/nginx/html/
# 权限
RUN chmod -R 755 /usr/share/nginx/html
FROM nginx:stable-alpine AS runtime
# 暴露80端口Docker声明方便容器映射不影响实际运行
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
# 复制自定义Nginx配置解决路由404+禁用所有缓存,核心修正)
COPY ./default.conf /etc/nginx/conf.d/default.conf
# 从构建阶段拷贝最终构建产物到Nginx静态资源根目录
COPY --from=builder /app/dist /usr/share/nginx/html
# Nginx官方镜像默认自动启动nginx无需额外CMD/ENTRYPOINT