Dockerfile.prod 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. # Stage 1: Dependencies
  2. FROM node:18-alpine AS deps
  3. WORKDIR /app
  4. # 安装 pnpm
  5. RUN npm install -g pnpm
  6. # 安装 bash 和其他必要的工具
  7. RUN apk add --no-cache libc6-compat bash
  8. # 复制 package.json 和 pnpm-lock.yaml(如果有的话)
  9. COPY package.json pnpm-lock.yaml* ./
  10. # 安装依赖
  11. RUN pnpm install --frozen-lockfile
  12. # Stage 2: Builder
  13. FROM node:18-alpine AS builder
  14. WORKDIR /app
  15. # 安装 pnpm 和 bash
  16. RUN npm install -g pnpm
  17. RUN apk add --no-cache bash
  18. # 复制所有文件
  19. COPY . .
  20. # 复制 node_modules
  21. COPY --from=deps /app/node_modules ./node_modules
  22. # 构建应用
  23. RUN pnpm build
  24. # Stage 3: Runner
  25. FROM node:18-alpine AS runner
  26. WORKDIR /app
  27. ENV NODE_ENV production
  28. # 安装 bash
  29. RUN apk add --no-cache bash
  30. # 创建非 root 用户
  31. RUN addgroup --system --gid 1001 nodejs
  32. RUN adduser --system --uid 1001 nextjs
  33. # 创建 uploads 目录并设置权限
  34. # RUN mkdir -p /app/public/uploads && chown -R nextjs:nodejs /app/public
  35. # 复制必要文件
  36. # COPY --from=builder /app/next.config.mjs ./
  37. # COPY --from=builder /app/public ./public
  38. # COPY --from=builder --chown=nextjs:nodejs /app/.next ./.next
  39. # COPY --from=builder /app/node_modules ./node_modules
  40. # COPY --from=builder /app/package.json ./package.json
  41. # 复制必要文件
  42. COPY --from=builder /app/next.config.mjs ./
  43. COPY --from=builder /app/public ./public
  44. COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
  45. COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
  46. # 复制 package.json(用于版本信息和脚本)
  47. COPY --from=builder /app/package.json ./package.json
  48. # 设置为非 root 用户
  49. USER nextjs
  50. # 暴露端口
  51. EXPOSE 3000
  52. ENV PORT 3000
  53. # 使用 bash 启动应用
  54. # CMD ["/bin/bash", "-c", "node_modules/.bin/next start"]
  55. CMD ["/bin/bash", "-c", "node server.js"]
  56. # # Stage 1: Dependencies
  57. # FROM node:alpine AS deps
  58. # # Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
  59. # RUN apk add --no-cache libc6-compat
  60. # WORKDIR /app
  61. # # 安装 pnpm
  62. # RUN npm install -g pnpm
  63. # COPY package.json pnpm-lock.yaml* ./
  64. # RUN pnpm install --frozen-lockfile
  65. # # Stage 2: Builder
  66. # FROM node:alpine AS builder
  67. # WORKDIR /app
  68. # # 安装 pnpm
  69. # RUN npm install -g pnpm
  70. # COPY . .
  71. # COPY --from=deps /app/node_modules ./node_modules
  72. # RUN pnpm build && pnpm install --prod --ignore-scripts --prefer-offline
  73. # # Stage 3: Runner
  74. # FROM node:alpine AS runner
  75. # WORKDIR /app
  76. # ENV NODE_ENV production
  77. # RUN addgroup -g 1001 -S nodejs
  78. # RUN adduser -S nextjs -u 1001
  79. # # You only need to copy next.config.js if you are NOT using the default configuration
  80. # # COPY --from=builder /app/next.config.js ./
  81. # COPY --from=builder /app/public ./public
  82. # COPY --from=builder --chown=nextjs:nodejs /app/.next ./.next
  83. # COPY --from=builder /app/node_modules ./node_modules
  84. # COPY --from=builder /app/package.json ./package.json
  85. # USER nextjs
  86. # EXPOSE 3000
  87. # ENV PORT 3000
  88. # # Next.js collects completely anonymous telemetry data about general usage.
  89. # # Learn more here: https://nextjs.org/telemetry
  90. # # Uncomment the following line in case you want to disable telemetry.
  91. # # ENV NEXT_TELEMETRY_DISABLED 1
  92. # CMD ["node", "node_modules/.bin/next", "start"]
  93. # # CMD ["/bin/bash", "-c", "node_modules/.bin/next start"]