Dockerfile 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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:20-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:20-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 yarn build
  24. # If using npm comment out above and use below instead
  25. # RUN npm run build
  26. # Production image, copy all the files and run next
  27. FROM node:20-alpine AS runner
  28. WORKDIR /app
  29. ENV NODE_ENV production
  30. # Uncomment the following line in case you want to disable telemetry during runtime.
  31. # ENV NEXT_TELEMETRY_DISABLED 1
  32. RUN addgroup --system --gid 1001 nodejs
  33. RUN adduser --system --uid 1001 nextjs
  34. COPY --from=builder /app/public ./public
  35. # Automatically leverage output traces to reduce image size
  36. # https://nextjs.org/docs/advanced-features/output-file-tracing
  37. COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
  38. COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
  39. USER nextjs
  40. EXPOSE 3000
  41. ENV PORT 3000
  42. CMD ["node", "server.js"]