build: Dockerfile 改单阶段(本地 dist + npm 装依赖),锁定 linux/amd64 适配绿联 NAS

This commit is contained in:
陈银军
2026-09-19 17:00:12 +08:00
parent c236df3e49
commit 72722ab6bf
2 changed files with 10 additions and 20 deletions
-1
View File
@@ -1,7 +1,6 @@
# node_modules 与产物 # node_modules 与产物
node_modules node_modules
frontend/node_modules frontend/node_modules
frontend/dist
backend/node_modules backend/node_modules
backend/data backend/data
manager-data manager-data
+10 -19
View File
@@ -1,30 +1,21 @@
# ============ 阶段一:构建前端(Vue3 + Vite) ============ # 本地已构建好 frontend/dist,镜像内无需 pnpm/corepack/构建工具链
FROM node:18-slim AS frontend-build FROM --platform=linux/amd64 node:18-slim
WORKDIR /build
RUN corepack enable
COPY frontend/package.json frontend/pnpm-lock.yaml ./
RUN pnpm install --frozen-lockfile
COPY frontend/ ./
RUN pnpm build
# ============ 阶段二:运行时(Express + better-sqlite3) ============
FROM node:18-slim
ENV NODE_ENV=production \ ENV NODE_ENV=production \
PORT=3000 \ PORT=3000 \
DATA_DIR=/app/data DATA_DIR=/app/data \
npm_config_registry=https://registry.npmmirror.com
WORKDIR /app WORKDIR /app
RUN corepack enable
# 后端依赖(better-sqlite3 在 node:18 有预编译二进制,无需编译工具链) # 后端依赖:仅 express + better-sqlite3(node:18 有预编译二进制)
COPY backend/package.json backend/pnpm-lock.yaml ./ COPY backend/package.json ./
RUN pnpm install --prod --frozen-lockfile RUN npm install --production
COPY backend/ ./ COPY backend/ ./
# 前端产物托管在 /frontend/dist(server.js 中 distDir 指向 ../frontend/dist) # 前端产物(本地构建好的 dist)
COPY --from=frontend-build /build/dist /frontend/dist COPY frontend/dist /frontend/dist
# 非 root 运行(§3.7 安全:UID 10000) # 非 root 运行
RUN mkdir -p /app/data && chown -R 10000:10000 /app/data RUN mkdir -p /app/data && chown -R 10000:10000 /app/data
USER 10000:10000 USER 10000:10000