Dockerfile.prod 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. # WORKDIR /app
  6. # COPY package.json yarn.lock ./
  7. # RUN yarn install --frozen-lockfile
  8. # # Rebuild the source code only when needed
  9. # FROM node:alpine AS builder
  10. # WORKDIR /app
  11. # COPY . .
  12. # COPY --from=deps /app/node_modules ./node_modules
  13. # RUN yarn build && yarn install --production --ignore-scripts --prefer-offline
  14. # # Production image, copy all the files and run next
  15. # FROM node:alpine AS runner
  16. # WORKDIR /app
  17. # ENV NODE_ENV production
  18. # RUN addgroup -g 1001 -S nodejs
  19. # RUN adduser -S nextjs -u 1001
  20. # # You only need to copy next.config.js if you are NOT using the default configuration
  21. # # COPY --from=builder /app/next.config.js ./
  22. # COPY --from=builder /app/public ./public
  23. # COPY --from=builder --chown=nextjs:nodejs /app/.next ./.next
  24. # COPY --from=builder /app/node_modules ./node_modules
  25. # COPY --from=builder /app/package.json ./package.json
  26. # USER nextjs
  27. # EXPOSE 3000
  28. # ENV PORT 3000
  29. # # Next.js collects completely anonymous telemetry data about general usage.
  30. # # Learn more here: https://nextjs.org/telemetry
  31. # # Uncomment the following line in case you want to disable telemetry.
  32. # # ENV NEXT_TELEMETRY_DISABLED 1
  33. # CMD ["node_modules/.bin/next", "start"]
  34. # Stage 1: Dependencies
  35. FROM node:alpine AS deps
  36. WORKDIR /app
  37. # 安装 pnpm
  38. RUN npm install -g pnpm
  39. # 安装 libc6-compat(如果需要的话)
  40. RUN apk add --no-cache libc6-compat
  41. # 复制 package.json 和 pnpm-lock.yaml(如果有的话)
  42. COPY package.json pnpm-lock.yaml* ./
  43. # 安装依赖
  44. RUN pnpm install --frozen-lockfile
  45. # Stage 2: Builder
  46. FROM node:alpine AS builder
  47. WORKDIR /app
  48. # 安装 pnpm
  49. RUN npm install -g pnpm
  50. # 复制所有文件
  51. COPY . .
  52. # 复制 node_modules
  53. COPY --from=deps /app/node_modules ./node_modules
  54. # 构建应用
  55. RUN pnpm build
  56. # Stage 3: Runner
  57. FROM node:alpine AS runner
  58. WORKDIR /app
  59. ENV NODE_ENV production
  60. # 创建非 root 用户
  61. RUN addgroup --system --gid 1001 nodejs
  62. RUN adduser --system --uid 1001 nextjs
  63. # 复制必要文件
  64. COPY --from=builder /app/public ./public
  65. COPY --from=builder --chown=nextjs:nodejs /app/.next ./.next
  66. COPY --from=builder /app/node_modules ./node_modules
  67. COPY --from=builder /app/package.json ./package.json
  68. # 设置为非 root 用户
  69. USER nextjs
  70. # 暴露端口
  71. EXPOSE 3000
  72. ENV PORT 3000
  73. # 禁用 Next.js 遥测(如果需要)
  74. # ENV NEXT_TELEMETRY_DISABLED 1
  75. # 启动应用
  76. CMD ["pnpm", "start"]