Dockerfile.prod 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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:18-alpine AS deps
  36. WORKDIR /app
  37. # 安装 pnpm
  38. RUN npm install -g pnpm
  39. # 安装 bash 和其他必要的工具
  40. RUN apk add --no-cache libc6-compat bash
  41. # 设置 pnpm 使用淘宝镜像
  42. RUN pnpm config set registry https://registry.npm.taobao.org
  43. # 复制 package.json 和 pnpm-lock.yaml(如果有的话)
  44. COPY package.json pnpm-lock.yaml* ./
  45. # 安装依赖
  46. RUN pnpm install --frozen-lockfile
  47. # Stage 2: Builder
  48. FROM node:18-alpine AS builder
  49. WORKDIR /app
  50. # 安装 pnpm 和 bash
  51. RUN npm install -g pnpm
  52. RUN apk add --no-cache bash
  53. # 复制所有文件
  54. COPY . .
  55. # 复制 node_modules
  56. COPY --from=deps /app/node_modules ./node_modules
  57. # 构建应用
  58. RUN pnpm build
  59. # Stage 3: Runner
  60. FROM node:18-alpine AS runner
  61. WORKDIR /app
  62. ENV NODE_ENV production
  63. # 安装 bash
  64. RUN apk add --no-cache bash
  65. # 创建非 root 用户
  66. RUN addgroup --system --gid 1001 nodejs
  67. RUN adduser --system --uid 1001 nextjs
  68. # 复制必要文件
  69. COPY --from=builder /app/public ./public
  70. COPY --from=builder --chown=nextjs:nodejs /app/.next ./.next
  71. COPY --from=builder /app/node_modules ./node_modules
  72. COPY --from=builder /app/package.json ./package.json
  73. # 设置为非 root 用户
  74. USER nextjs
  75. # 暴露端口
  76. EXPOSE 3000
  77. ENV PORT 3000
  78. # 使用 bash 启动应用
  79. CMD ["/bin/bash", "-c", "node_modules/.bin/next start"]