Dockerfile.prod 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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"]
  40. # Install dependencies only when needed
  41. FROM node:18-alpine AS deps
  42. # Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
  43. RUN apk add --no-cache libc6-compat
  44. RUN npm install -g pnpm
  45. WORKDIR /app
  46. # 复制 pnpm 相关文件
  47. COPY package.json pnpm-lock.yaml* .npmrc* ./
  48. # 使用 pnpm 安装依赖
  49. RUN pnpm install --frozen-lockfile
  50. # Rebuild the source code only when needed
  51. FROM node:18-alpine AS builder
  52. WORKDIR /app
  53. # 安装 pnpm
  54. RUN npm install -g pnpm
  55. # 复制依赖和源代码
  56. COPY --from=deps /app/node_modules ./node_modules
  57. COPY . .
  58. # 使用构建参数
  59. ARG MONGODB_URI
  60. ENV MONGODB_URI=$MONGODB_URI
  61. # 使用 pnpm 构建
  62. RUN pnpm build
  63. # Production image, copy all the files and run next
  64. FROM node:18-alpine AS runner
  65. WORKDIR /app
  66. ENV NODE_ENV production
  67. # 安装 dos2unix
  68. RUN apk add --no-cache dos2unix
  69. RUN addgroup -g 1001 -S nodejs
  70. RUN adduser -S nextjs -u 1001
  71. # 复制必要文件
  72. COPY --from=builder /app/public ./public
  73. COPY --from=builder /app/package.json ./package.json
  74. COPY --from=builder /app/node_modules ./node_modules
  75. COPY --from=builder --chown=nextjs:nodejs /app/.next ./.next
  76. COPY --from=builder /app/next.config.mjs ./next.config.mjs
  77. # 修复可能的行结束符问题
  78. RUN dos2unix /app/node_modules/.bin/next
  79. # 设置正确的权限
  80. RUN chown -R nextjs:nodejs /app
  81. USER nextjs
  82. EXPOSE 3000
  83. ENV PORT 3000
  84. ENV NEXT_TELEMETRY_DISABLED 1
  85. # 使用 pnpm 启动应用
  86. CMD ["pnpm", "start"]