# Stage 1: Install dependencies FROM node:22-alpine AS deps WORKDIR /app # Install dependencies based on your package manager # If you use npm: COPY package.json package-lock.json ./ RUN npm ci # If you use yarn: # COPY package.json yarn.lock ./ # RUN yarn install --frozen-lockfile # Stage 2: Build the Next.js application FROM node:22-alpine AS builder WORKDIR /app # Copy dependencies from the 'deps' stage COPY --from=deps /app/node_modules ./node_modules COPY . . # Build the Next.js application # This will create the .next directory with optimized production build RUN npm run build # Stage 3: Run the Next.js application in production FROM node:22-alpine AS runner WORKDIR /app # Set environment variables for Next.js production server ENV NODE_ENV production # Next.js requires specific environment variables for standalone output # If you are using `output: 'standalone'` in next.config.js, uncomment the following: # ENV NEXT_SHARP_PATH=/usr/local/lib/node_modules/sharp # COPY --from=builder /app/.next/standalone ./ # COPY --from=builder /app/.next/static ./.next/static # COPY --from=builder /app/public ./public # If not using standalone output, copy the necessary files COPY --from=builder /app/public ./public COPY --from=builder /app/.next ./.next COPY --from=builder /app/node_modules ./node_modules COPY --from=builder /app/package.json ./package.json # Expose the port Next.js runs on (default is 3000) EXPOSE 3000 # Command to run the Next.js application CMD ["npm", "start"]