Dockerfile 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. # Copied and adapted from https://github.com/vercel/next.js/tree/canary/examples/with-docker
  2. # Install dependencies only when needed
  3. FROM node:22-alpine AS deps
  4. # Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
  5. RUN apk add --no-cache libc6-compat
  6. WORKDIR /app
  7. # Install dependencies based on the preferred package manager
  8. COPY .yarnrc.yml package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./
  9. RUN \
  10. # Configure Corepack
  11. corepack enable; \
  12. corepack prepare --activate;
  13. RUN yarn install --immutable
  14. # Rebuild the source code only when needed
  15. FROM node:22-alpine AS builder
  16. WORKDIR /app
  17. COPY --from=deps /app/node_modules ./node_modules
  18. COPY . .
  19. # Next.js collects completely anonymous telemetry data about general usage.
  20. # Learn more here: https://nextjs.org/telemetry
  21. # Uncomment the following line in case you want to disable telemetry during the build.
  22. # ENV NEXT_TELEMETRY_DISABLED 1
  23. RUN \
  24. # Configure Corepack
  25. corepack enable; \
  26. corepack prepare --activate;
  27. RUN yarn build
  28. # If using npm comment out above and use below instead
  29. # RUN npm run build
  30. # Production image, copy all the files and run next
  31. FROM node:22-alpine AS runner
  32. WORKDIR /app
  33. ENV NODE_ENV production
  34. # Uncomment the following line in case you want to disable telemetry during runtime.
  35. # ENV NEXT_TELEMETRY_DISABLED 1
  36. RUN addgroup --system --gid 1001 nodejs
  37. RUN adduser --system --uid 1001 nextjs
  38. COPY --from=builder /app/public ./public
  39. # Automatically leverage output traces to reduce image size
  40. # https://nextjs.org/docs/advanced-features/output-file-tracing
  41. COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
  42. COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
  43. USER nextjs
  44. EXPOSE 3000
  45. ENV PORT 3000
  46. CMD ["node", "server.js"]