diff --git a/dockerfile b/dockerfile index 4be51a1..2f9804a 100644 --- a/dockerfile +++ b/dockerfile @@ -1,122 +1,48 @@ -# ============================================================================= -# Dockerfile for Next.js 16 - Production Optimized for Coolify -# ============================================================================= -# Multi-stage build for minimal image size and maximum performance -# Compatible with: Coolify, Docker Compose, Kubernetes -# Node Version: 20 LTS (Alpine) -# Package Manager: npm (native to project) -# ============================================================================= +# Dockerfile for Next.js 14 - Optimized for Production -# ----------------------------------------------------------------------------- -# Stage 1: Dependencies (deps) -# Install only production dependencies with locked versions -# ----------------------------------------------------------------------------- -FROM node:20-alpine AS deps - -# Install system dependencies required by Node.js native modules -RUN apk add --no-cache libc6-compat +# Use the official Node.js image with Alpine Linux for a smaller footprint +FROM node:22-alpine AS base +# Set the working directory in the container WORKDIR /app -# Copy package files for dependency installation -COPY package.json package-lock.json ./ +# Copy package.json and package-lock.json (or yarn.lock) +COPY package*.json ./ -# Install dependencies with npm ci (clean install from lock file) -# This ensures reproducible builds and faster installs -RUN npm ci --only=production --ignore-scripts && \ - npm cache clean --force +# Install dependencies - leveraging Docker cache +RUN npm i --force -# ----------------------------------------------------------------------------- -# Stage 2: Builder (builder) -# Build the Next.js application with all dev dependencies -# ----------------------------------------------------------------------------- -FROM node:20-alpine AS builder - -# Install system dependencies for Prisma and build tools -RUN apk add --no-cache \ - libc6-compat \ - openssl - -WORKDIR /app - -# Copy package files -COPY package.json package-lock.json ./ - -# Install ALL dependencies (including devDependencies for build) -RUN npm ci --ignore-scripts && \ - npm cache clean --force - -# Copy application source +# Copy the rest of the application code COPY . . -# Set build-time environment variables -ENV NEXT_TELEMETRY_DISABLED=1 -ENV NODE_ENV=production - -# Generate Prisma Client -RUN npx prisma generate - -# Build Next.js application with standalone output -# This creates a minimal server bundle in .next/standalone +# Build the Next.js application RUN npm run build -# ----------------------------------------------------------------------------- -# Stage 3: Runner (production) -# Minimal runtime image with only necessary files -# ----------------------------------------------------------------------------- -FROM node:20-alpine AS runner - -# Install runtime dependencies -RUN apk add --no-cache \ - libc6-compat \ - openssl \ - wget \ - dumb-init && \ - rm -rf /var/cache/apk/* +# Stage 2: Production image - smaller and leaner +FROM node:22-alpine AS runner +# Set the working directory WORKDIR /app -# Set production environment variables -ENV NODE_ENV=production -ENV NEXT_TELEMETRY_DISABLED=1 -ENV PORT=3000 -ENV HOSTNAME=0.0.0.0 +# Set environment variables +ENV NODE_ENV $NODE_ENV +ENV NEXT_TELEMETRY_DISABLED 1 -# Create non-root user for security -RUN addgroup --system --gid 1001 nodejs && \ - adduser --system --uid 1001 nextjs +# Add a non-root user for security +RUN addgroup -g 1001 -S nodejs +RUN adduser -S nextjs -u 1001 -# Copy Prisma runtime files -COPY --from=builder --chown=nextjs:nodejs /app/node_modules/.prisma ./node_modules/.prisma -COPY --from=builder --chown=nextjs:nodejs /app/node_modules/@prisma ./node_modules/@prisma +# Copy only the necessary files from the builder stage +COPY --from=base /app/next.config.js ./ +COPY --from=base /app/public ./public +COPY --from=base --chown=nextjs:nodejs /app/.next/standalone ./ +COPY --from=base --chown=nextjs:nodejs /app/.next/static ./.next/static -# Copy standalone Next.js server -COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./ - -# Copy static files -COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static - -# Copy public directory -COPY --from=builder --chown=nextjs:nodejs /app/public ./public - -# Copy Prisma schema for runtime (if needed) -COPY --from=builder --chown=nextjs:nodejs /app/prisma ./prisma - -# Copy scripts directory for seed and admin tools -COPY --from=builder --chown=nextjs:nodejs /app/scripts ./scripts - -# Switch to non-root user +# Change ownership of all copied files to the non-root user USER nextjs -# Expose application port +# Expose the port Next.js listens on EXPOSE 3000 -# Health check for container orchestration -HEALTHCHECK --interval=30s --timeout=10s --start-period=40s --retries=3 \ - CMD wget --no-verbose --tries=1 --spider http://localhost:3000/api/health || exit 1 - -# Use dumb-init to handle signals properly -ENTRYPOINT ["dumb-init", "--"] - -# Start the Next.js server +# Command to start the Next.js server in production mode CMD ["node", "server.js"] \ No newline at end of file