Dockerfile 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. # To use this Dockerfile, you have to set `output: 'standalone'` in your next.config.mjs file.
  2. # From https://github.com/vercel/next.js/blob/canary/examples/with-docker/Dockerfile
  3. FROM node:22.17.0-alpine AS base
  4. # Install dependencies only when needed
  5. FROM base AS deps
  6. # Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
  7. RUN apk add --no-cache libc6-compat
  8. WORKDIR /app
  9. # Install dependencies using pnpm
  10. COPY package.json pnpm-lock.yaml* ./
  11. RUN corepack enable && corepack prepare pnpm@10.33.0 --activate && pnpm i --frozen-lockfile
  12. # Rebuild the source code only when needed
  13. FROM base AS builder
  14. WORKDIR /app
  15. COPY --from=deps /app/node_modules ./node_modules
  16. COPY . .
  17. # Next.js collects completely anonymous telemetry data about general usage.
  18. # Learn more here: https://nextjs.org/telemetry
  19. # Uncomment the following line in case you want to disable telemetry during the build.
  20. # ENV NEXT_TELEMETRY_DISABLED 1
  21. RUN corepack enable && corepack prepare pnpm@10.33.0 --activate && pnpm run build
  22. # Production image, copy all the files and run next
  23. FROM base AS runner
  24. WORKDIR /app
  25. ENV NODE_ENV production
  26. # Uncomment the following line in case you want to disable telemetry during runtime.
  27. # ENV NEXT_TELEMETRY_DISABLED 1
  28. RUN addgroup --system --gid 1001 nodejs
  29. RUN adduser --system --uid 1001 nextjs
  30. # Set the correct permission for prerender cache
  31. RUN mkdir .next
  32. RUN chown nextjs:nodejs .next
  33. # Automatically leverage output traces to reduce image size
  34. # https://nextjs.org/docs/advanced-features/output-file-tracing
  35. COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
  36. COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
  37. USER nextjs
  38. EXPOSE 3000
  39. ENV PORT 3000
  40. # server.js is created by next build from the standalone output
  41. # https://nextjs.org/docs/pages/api-reference/next-config-js/output
  42. CMD HOSTNAME="0.0.0.0" node server.js