Dockerfile.prod 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. # Install dependencies only when needed
  2. FROM node:alpine AS deps
  3. # Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
  4. RUN apk add --no-cache libc6-compat
  5. RUN npm install -g pnpm
  6. WORKDIR /app
  7. # 复制 pnpm 相关文件
  8. COPY package.json pnpm-lock.yaml .npmrc* ./
  9. # 使用 pnpm 安装依赖
  10. RUN pnpm install --frozen-lockfile
  11. # Rebuild the source code only when needed
  12. FROM node:alpine AS builder
  13. WORKDIR /app
  14. # 安装 pnpm
  15. RUN npm install -g pnpm
  16. # 复制依赖
  17. COPY --from=deps /app/node_modules ./node_modules
  18. COPY . .
  19. # 使用构建参数
  20. ARG MONGODB_URI
  21. ENV MONGODB_URI=$MONGODB_URI
  22. # 使用 pnpm 构建
  23. RUN pnpm build
  24. # Production image, copy all the files and run next
  25. FROM node:alpine AS runner
  26. WORKDIR /app
  27. ENV NODE_ENV production
  28. RUN addgroup -g 1001 -S nodejs
  29. RUN adduser -S nextjs -u 1001
  30. # 复制必要文件
  31. COPY --from=builder /app/public ./public
  32. COPY --from=builder /app/package.json ./package.json
  33. COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
  34. COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
  35. USER nextjs
  36. EXPOSE 3000
  37. ENV PORT 3000
  38. ENV NEXT_TELEMETRY_DISABLED 1
  39. CMD ["sh", "-c", "if [ -d .next/standalone ]; then node .next/standalone/server.js; else node_modules/.bin/next start; fi"]