From 34d76a0a50387306f0417ade21fca1f46759d63a Mon Sep 17 00:00:00 2001 From: ywp Date: Wed, 22 Oct 2025 15:21:33 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20Dockerfile?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Dockerfile | 37 +++++++++++++++++++++++++++---------- 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/Dockerfile b/Dockerfile index c663165..fce309b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,39 +1,56 @@ -# ---- Build stage ---- +# =============================== +# ① 构建阶段:安装依赖 + 拷贝代码 +# =============================== FROM docker.1ms.run/node:20-slim AS build WORKDIR /app +# 安装顶层依赖(如果有前端构建) COPY package*.json ./ RUN npm ci +# 拷贝前端、后端、静态资源 COPY dist/ ./dist COPY public/ ./public COPY server/ ./server COPY src/ ./src COPY index.html ./ -# ---- Runtime stage ---- -FROM docker.1ms.run/nginx:1.27-alpine +# 可选:前端构建(如果你有 vite、react、vue 项目) +# 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 -# 拷贝 Node.js 后端 +# 拷贝后端代码和依赖 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/public /usr/share/nginx/html/public COPY --from=build /app/index.html /usr/share/nginx/html/ -# Nginx 配置 +# 拷贝 Nginx 配置和启动脚本 COPY nginx.conf /etc/nginx/nginx.conf - -# 启动脚本 COPY start.sh /start.sh RUN chmod +x /start.sh EXPOSE 80 - CMD ["/start.sh"]