alexcdev1 6 сар өмнө
parent
commit
9435379e74
1 өөрчлөгдсөн 32 нэмэгдсэн , 2 устгасан
  1. 32 2
      Dockerfile.prod

+ 32 - 2
Dockerfile.prod

@@ -54,7 +54,37 @@
 
 # CMD ["sh", "-c", "if [ -d .next/standalone ]; then node .next/standalone/server.js; else node_modules/.bin/next start; fi"]
 
-# ... [前面的部分保持不变] ...
+# Install dependencies only when needed
+FROM node:18-alpine AS deps
+# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
+RUN apk add --no-cache libc6-compat
+RUN npm install -g pnpm
+
+WORKDIR /app
+
+# 复制 pnpm 相关文件
+COPY package.json pnpm-lock.yaml* .npmrc* ./
+
+# 使用 pnpm 安装依赖
+RUN pnpm install --frozen-lockfile
+
+# Rebuild the source code only when needed
+FROM node:18-alpine AS builder
+WORKDIR /app
+
+# 安装 pnpm
+RUN npm install -g pnpm
+
+# 复制依赖和源代码
+COPY --from=deps /app/node_modules ./node_modules
+COPY . .
+
+# 使用构建参数
+ARG MONGODB_URI
+ENV MONGODB_URI=$MONGODB_URI
+
+# 使用 pnpm 构建
+RUN pnpm build
 
 # Production image, copy all the files and run next
 FROM node:18-alpine AS runner
@@ -62,7 +92,7 @@ WORKDIR /app
 
 ENV NODE_ENV production
 
-# 安装 dos2unix 并修复可能的行结束符问题
+# 安装 dos2unix
 RUN apk add --no-cache dos2unix
 
 RUN addgroup -g 1001 -S nodejs