Dockerfile 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. # syntax=docker.io/docker/dockerfile:1
  2. FROM node:18-alpine AS base
  3. # Install dependencies only when needed
  4. FROM base AS deps
  5. # Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
  6. RUN apk add --no-cache libc6-compat
  7. WORKDIR /app
  8. # Install dependencies based on the preferred package manager
  9. COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* .npmrc* ./
  10. RUN \
  11. if [ -f yarn.lock ]; then yarn --frozen-lockfile; \
  12. elif [ -f package-lock.json ]; then npm ci; \
  13. elif [ -f pnpm-lock.yaml ]; then corepack enable pnpm && pnpm i --frozen-lockfile; \
  14. else echo "Lockfile not found." && exit 1; \
  15. fi
  16. # Rebuild the source code only when needed
  17. FROM base AS builder
  18. WORKDIR /app
  19. COPY --from=deps /app/node_modules ./node_modules
  20. COPY . .
  21. # Next.js collects completely anonymous telemetry data about general usage.
  22. # Learn more here: https://nextjs.org/telemetry
  23. # Uncomment the following line in case you want to disable telemetry during the build.
  24. # ENV NEXT_TELEMETRY_DISABLED=1
  25. RUN \
  26. if [ -f yarn.lock ]; then yarn run build; \
  27. elif [ -f package-lock.json ]; then npm run build; \
  28. elif [ -f pnpm-lock.yaml ]; then corepack enable pnpm && pnpm run build; \
  29. else echo "Lockfile not found." && exit 1; \
  30. fi
  31. # Production image, copy all the files and run next
  32. FROM base AS runner
  33. WORKDIR /app
  34. ENV NODE_ENV=production
  35. # Uncomment the following line in case you want to disable telemetry during runtime.
  36. # ENV NEXT_TELEMETRY_DISABLED=1
  37. RUN addgroup --system --gid 1001 nodejs
  38. RUN adduser --system --uid 1001 nextjs
  39. COPY --from=builder /app/public ./public
  40. # Automatically leverage output traces to reduce image size
  41. # https://nextjs.org/docs/advanced-features/output-file-tracing
  42. COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
  43. COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
  44. USER nextjs
  45. EXPOSE 3000
  46. ENV PORT=3000
  47. # server.js is created by next build from the standalone output
  48. # https://nextjs.org/docs/pages/api-reference/config/next-config-js/output
  49. ENV HOSTNAME="0.0.0.0"
  50. CMD ["node", "server.js"]