# Stage 1: Build stage FROM node:18-alpine AS builder WORKDIR /app # Copy package files COPY package*.json ./ COPY package-lock.json* ./ # Install all dependencies (including dev dependencies) RUN npm ci # Copy source code COPY . . # Build the application RUN npm run build # Stage 2: Production stage FROM node:18-alpine AS runner WORKDIR /app # Create non-root user for security RUN addgroup --system --gid 1001 nodejs RUN adduser --system --uid 1001 nextjs # Copy necessary files from builder stage 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 # Set proper permissions RUN chown -R nextjs:nodejs /app # Switch to non-root user USER nextjs # Expose port EXPOSE 3000 # Set environment variable ENV PORT 3000 ENV HOSTNAME "0.0.0.0" # Start the application CMD ["npm", "start"]