Dockerfile 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # Copied from https://github.com/vercel/next.js/tree/canary/examples/with-docker
  2. # Install dependencies only when needed
  3. FROM node:18-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 package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./
  9. RUN \
  10. if [ -f yarn.lock ]; then yarn --frozen-lockfile; \
  11. elif [ -f package-lock.json ]; then npm ci; \
  12. elif [ -f pnpm-lock.yaml ]; then yarn global add pnpm && pnpm i --frozen-lockfile; \
  13. else echo "Lockfile not found." && exit 1; \
  14. fi
  15. # Rebuild the source code only when needed
  16. FROM node:18-alpine AS builder
  17. WORKDIR /app
  18. COPY --from=deps /app/node_modules ./node_modules
  19. COPY . .
  20. # Next.js collects completely anonymous telemetry data about general usage.
  21. # Learn more here: https://nextjs.org/telemetry
  22. # Uncomment the following line in case you want to disable telemetry during the build.
  23. # ENV NEXT_TELEMETRY_DISABLED 1
  24. RUN yarn build
  25. # If using npm comment out above and use below instead
  26. # RUN npm run build
  27. # Production image, copy all the files and run next
  28. FROM node:18-alpine AS runner
  29. WORKDIR /app
  30. ENV NODE_ENV production
  31. # Uncomment the following line in case you want to disable telemetry during runtime.
  32. # ENV NEXT_TELEMETRY_DISABLED 1
  33. RUN addgroup --system --gid 1001 nodejs
  34. RUN adduser --system --uid 1001 nextjs
  35. COPY --from=builder /app/public ./public
  36. # Automatically leverage output traces to reduce image size
  37. # https://nextjs.org/docs/advanced-features/output-file-tracing
  38. COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
  39. COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
  40. USER nextjs
  41. EXPOSE 3000
  42. ENV PORT 3000
  43. CMD ["node", "server.js"]