# # Install dependencies only when needed # FROM node: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: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:alpine AS runner # WORKDIR /app # ENV NODE_ENV production # RUN addgroup -g 1001 -S nodejs # RUN adduser -S nextjs -u 1001 # # 复制必要文件 # COPY --from=builder /app/public ./public # COPY --from=builder /app/package.json ./package.json # COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./ # COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static # USER nextjs # EXPOSE 3000 # ENV PORT 3000 # ENV NEXT_TELEMETRY_DISABLED 1 # 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 WORKDIR /app ENV NODE_ENV production # 安装 pnpm RUN npm install -g pnpm # 安装 dos2unix RUN apk add --no-cache dos2unix RUN addgroup -g 1001 -S nodejs RUN adduser -S nextjs -u 1001 # 复制必要文件 COPY --from=builder /app/public ./public COPY --from=builder /app/package.json ./package.json COPY --from=builder /app/pnpm-lock.yaml ./pnpm-lock.yaml COPY --from=builder /app/node_modules ./node_modules COPY --from=builder --chown=nextjs:nodejs /app/.next ./.next COPY --from=builder /app/next.config.mjs ./next.config.mjs # 修复可能的行结束符问题 RUN dos2unix /app/node_modules/.bin/next # 设置正确的权限 RUN chown -R nextjs:nodejs /app USER nextjs EXPOSE 3000 ENV PORT 3000 ENV NEXT_TELEMETRY_DISABLED 1 # 使用 node 直接启动 Next.js CMD ["node", "node_modules/.bin/next", "start"]