# 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"]