Dockerfile 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. FROM node:18-alpine AS base
  2. # Install dependencies only when needed
  3. FROM base 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 base 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 base 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. # Set the correct permission for prerender cache
  37. RUN mkdir .next
  38. RUN chown nextjs:nodejs .next
  39. COPY --from=builder --chown=nextjs:nodejs /app/ /app/
  40. USER nextjs
  41. EXPOSE 3000
  42. ENV PORT 3000
  43. # set hostname to localhost
  44. ENV HOSTNAME "0.0.0.0"
  45. # server.js is created by next build from the standalone output
  46. # https://nextjs.org/docs/pages/api-reference/next-config-js/output
  47. CMD ["npm", "run", "start"]