# Optimized Dockerfile for Next.js on Coolify # Receive build arguments passed by Coolify for the builder stage ARG NEXT_PUBLIC_SUPABASE_URL ARG NEXT_PUBLIC_SUPABASE_ANON_KEY # Builder stage FROM node:20-alpine AS deps WORKDIR /app # Install dependencies only when needed (better layer caching) COPY myfavstuff/package.json myfavstuff/package-lock.json* ./ # Install dependencies using frozen lockfile for consistent builds # Use clean-install for production dependencies only RUN npm ci --only=production # Rebuild the source code only when needed FROM node:20-alpine AS builder WORKDIR /app # These ARGs are available to RUN commands in this stage ARG NEXT_PUBLIC_SUPABASE_URL ARG NEXT_PUBLIC_SUPABASE_ANON_KEY ENV NODE_ENV=production # Copy dependencies from deps stage COPY --from=deps /app/node_modules ./node_modules COPY myfavstuff/package.json ./package.json # Copy source files COPY myfavstuff/. . # Set Next.js build memory limit to avoid OOM issues ENV NODE_OPTIONS="--max_old_space_size=2048" # Run the build with environment variables RUN NEXT_PUBLIC_SUPABASE_URL=$NEXT_PUBLIC_SUPABASE_URL \ NEXT_PUBLIC_SUPABASE_ANON_KEY=$NEXT_PUBLIC_SUPABASE_ANON_KEY \ npm run build # Production stage (runner) - using smaller base image for runtime FROM node:20-alpine AS runner WORKDIR /app # Use production node environment ENV NODE_ENV=production # Runtime environment variables will be set by Coolify directly in the container environment # Create a non-root user for security RUN addgroup -g 1001 -S nodejs && adduser -S nextjs -u 1001 # Set working directory ownership RUN chown -R nextjs:nodejs /app # Optimize production image size by only copying what's needed COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./ COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static COPY --from=builder --chown=nextjs:nodejs /app/public ./public # Switch to non-root user for better security USER nextjs # Expose the running port EXPOSE 3000 # Set server runtime environment ENV PORT=3000 ENV HOSTNAME=0.0.0.0 # Optimize for containerized environment ENV NODE_OPTIONS="--enable-source-maps --max-http-header-size=16384" # Use exec form for CMD to properly handle signals CMD ["node", "server.js"]