Dockerfile 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 based on the preferred package manager
  10. COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./
  11. RUN \
  12. if [ -f yarn.lock ]; then yarn --frozen-lockfile; \
  13. elif [ -f package-lock.json ]; then npm ci; \
  14. elif [ -f pnpm-lock.yaml ]; then corepack enable pnpm && pnpm i --frozen-lockfile; \
  15. else echo "Lockfile not found." && exit 1; \
  16. fi
  17. # Rebuild the source code only when needed
  18. FROM base AS builder
  19. WORKDIR /app
  20. COPY --from=deps /app/node_modules ./node_modules
  21. COPY . .
  22. # Next.js collects completely anonymous telemetry data about general usage.
  23. # Learn more here: https://nextjs.org/telemetry
  24. # Uncomment the following line in case you want to disable telemetry during the build.
  25. # ENV NEXT_TELEMETRY_DISABLED 1
  26. RUN \
  27. if [ -f yarn.lock ]; then yarn run build; \
  28. elif [ -f package-lock.json ]; then npm run build; \
  29. elif [ -f pnpm-lock.yaml ]; then corepack enable pnpm && pnpm run build; \
  30. else echo "Lockfile not found." && exit 1; \
  31. fi
  32. # Production image, copy all the files and run next
  33. FROM base AS runner
  34. WORKDIR /app
  35. ENV NODE_ENV production
  36. # Uncomment the following line in case you want to disable telemetry during runtime.
  37. # ENV NEXT_TELEMETRY_DISABLED 1
  38. RUN addgroup --system --gid 1001 nodejs
  39. RUN adduser --system --uid 1001 nextjs
  40. # Remove this line if you do not have this folder
  41. COPY --from=builder /app/public ./public
  42. # Set the correct permission for prerender cache
  43. RUN mkdir .next
  44. RUN chown nextjs:nodejs .next
  45. # Automatically leverage output traces to reduce image size
  46. # https://nextjs.org/docs/advanced-features/output-file-tracing
  47. COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
  48. COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
  49. USER nextjs
  50. EXPOSE 3001
  51. ENV PORT 3001
  52. # server.js is created by next build from the standalone output
  53. # https://nextjs.org/docs/pages/api-reference/next-config-js/output
  54. CMD HOSTNAME="0.0.0.0" node server.js