更新 Dockerfile

This commit is contained in:
ywp
2025-10-22 15:21:33 +08:00
parent 5fb68efd00
commit 34d76a0a50

View File

@ -1,39 +1,56 @@
# ---- Build stage ---- # ===============================
# ① 构建阶段:安装依赖 + 拷贝代码
# ===============================
FROM docker.1ms.run/node:20-slim AS build FROM docker.1ms.run/node:20-slim AS build
WORKDIR /app WORKDIR /app
# 安装顶层依赖(如果有前端构建)
COPY package*.json ./ COPY package*.json ./
RUN npm ci RUN npm ci
# 拷贝前端、后端、静态资源
COPY dist/ ./dist COPY dist/ ./dist
COPY public/ ./public COPY public/ ./public
COPY server/ ./server COPY server/ ./server
COPY src/ ./src COPY src/ ./src
COPY index.html ./ COPY index.html ./
# ---- Runtime stage ---- # 可选:前端构建(如果你有 vite、react、vue 项目)
FROM docker.1ms.run/nginx:1.27-alpine # RUN npm run build
RUN apk add --no-cache nodejs npm # ===============================
# ② 后端依赖安装阶段
# ===============================
FROM docker.1ms.run/node:20-slim AS server_deps
WORKDIR /app/server
COPY server/package*.json ./
RUN if [ -f package.json ]; then npm ci --omit=dev; fi
# ===============================
# ③ 运行阶段Nginx + Node 同容器)
# ===============================
FROM nginx:1.27-alpine
# 安装 Node.js给后端运行
RUN apk add --no-cache nodejs npm bash
WORKDIR /app WORKDIR /app
# 拷贝 Node.js 后端 # 拷贝后端代码和依赖
COPY --from=build /app/server /app/server COPY --from=build /app/server /app/server
COPY --from=server_deps /app/server/node_modules /app/server/node_modules
# 拷贝前端静态文件 # 拷贝前端静态资源
COPY --from=build /app/dist /usr/share/nginx/html COPY --from=build /app/dist /usr/share/nginx/html
COPY --from=build /app/public /usr/share/nginx/html/public COPY --from=build /app/public /usr/share/nginx/html/public
COPY --from=build /app/index.html /usr/share/nginx/html/ COPY --from=build /app/index.html /usr/share/nginx/html/
# Nginx 配置 # 拷贝 Nginx 配置和启动脚本
COPY nginx.conf /etc/nginx/nginx.conf COPY nginx.conf /etc/nginx/nginx.conf
# 启动脚本
COPY start.sh /start.sh COPY start.sh /start.sh
RUN chmod +x /start.sh RUN chmod +x /start.sh
EXPOSE 80 EXPOSE 80
CMD ["/start.sh"] CMD ["/start.sh"]