# Stage 1: Build FROM node:18-alpine AS builder WORKDIR /app # Copy package files COPY package.json package-lock.json ./ # Install ALL dependencies (including dev dependencies for build) RUN npm ci # Copy source code COPY . . # Build arguments ARG NEXT_PUBLIC_API_URL ENV NEXT_PUBLIC_API_URL=${NEXT_PUBLIC_API_URL} # Build the app RUN npm run build # Stage 2: Run FROM node:18-alpine AS runner WORKDIR /app # Set to production ENV NODE_ENV=production # Create nextjs user and group RUN addgroup --system --gid 1001 nodejs && \ adduser --system --uid 1001 nextjs # Copy necessary files from builder COPY --from=builder /app/public ./public COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./ COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static # Create cache directory and set permissions RUN mkdir -p .next/cache/images && \ chown -R nextjs:nodejs .next # Ensure the entire app directory has proper permissions RUN chown -R nextjs:nodejs /app # Switch to non-root user USER nextjs # Expose port EXPOSE 3000 ENV PORT=3000 ENV HOSTNAME="0.0.0.0" # Start the app CMD ["node", "server.js"]