Dockerfile 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. 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* .npmrc* ./
  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 corepack enable 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. # Disable telemetry during the build
  21. ENV NEXT_TELEMETRY_DISABLED 1
  22. # Build Next.js
  23. RUN \
  24. if [ -f yarn.lock ]; then NEXT_TELEMETRY_DISABLED=1 yarn run build; \
  25. elif [ -f package-lock.json ]; then NEXT_TELEMETRY_DISABLED=1 npm run build; \
  26. elif [ -f pnpm-lock.yaml ]; then corepack enable pnpm && NEXT_TELEMETRY_DISABLED=1 pnpm run build; \
  27. else echo "Lockfile not found." && exit 1; \
  28. fi
  29. # Verify the standalone directory exists
  30. RUN ls -la .next/standalone || (echo "Standalone directory not found" && exit 1)
  31. # Production image, copy all the files and run next
  32. FROM base AS runner
  33. WORKDIR /app
  34. ENV NODE_ENV production
  35. ENV NEXT_TELEMETRY_DISABLED 1
  36. RUN addgroup --system --gid 1001 nodejs
  37. RUN adduser --system --uid 1001 nextjs
  38. # Copy necessary files
  39. COPY --from=builder /app/public ./public
  40. COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
  41. COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
  42. USER nextjs
  43. ENV PORT=3000
  44. ENV HOSTNAME="0.0.0.0"
  45. CMD ["node", "server.js"]