refactor: update Dockerfile for Next.js 16 compatibility and optimize build process

This commit is contained in:
hamed
2025-12-10 11:00:57 +03:30
parent 76e96f1724
commit a8d4d5c090
+27 -101
View File
@@ -1,122 +1,48 @@
# ============================================================================= # Dockerfile for Next.js 14 - Optimized for Production
# 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)
# =============================================================================
# ----------------------------------------------------------------------------- # Use the official Node.js image with Alpine Linux for a smaller footprint
# Stage 1: Dependencies (deps) FROM node:22-alpine AS base
# 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
# Set the working directory in the container
WORKDIR /app WORKDIR /app
# Copy package files for dependency installation # Copy package.json and package-lock.json (or yarn.lock)
COPY package.json package-lock.json ./ COPY package*.json ./
# Install dependencies with npm ci (clean install from lock file) # Install dependencies - leveraging Docker cache
# This ensures reproducible builds and faster installs RUN npm i --force
RUN npm ci --only=production --ignore-scripts && \
npm cache clean --force
# ----------------------------------------------------------------------------- # Copy the rest of the application code
# 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 . . COPY . .
# Set build-time environment variables # Build the Next.js application
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
RUN npm run build RUN npm run build
# ----------------------------------------------------------------------------- # Stage 2: Production image - smaller and leaner
# Stage 3: Runner (production) FROM node:22-alpine AS runner
# 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/*
# Set the working directory
WORKDIR /app WORKDIR /app
# Set production environment variables # Set environment variables
ENV NODE_ENV=production ENV NODE_ENV $NODE_ENV
ENV NEXT_TELEMETRY_DISABLED=1 ENV NEXT_TELEMETRY_DISABLED 1
ENV PORT=3000
ENV HOSTNAME=0.0.0.0
# Create non-root user for security # Add a non-root user for security
RUN addgroup --system --gid 1001 nodejs && \ RUN addgroup -g 1001 -S nodejs
adduser --system --uid 1001 nextjs RUN adduser -S nextjs -u 1001
# Copy Prisma runtime files # Copy only the necessary files from the builder stage
COPY --from=builder --chown=nextjs:nodejs /app/node_modules/.prisma ./node_modules/.prisma COPY --from=base /app/next.config.js ./
COPY --from=builder --chown=nextjs:nodejs /app/node_modules/@prisma ./node_modules/@prisma 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 # Change ownership of all copied files to the non-root user
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
USER nextjs USER nextjs
# Expose application port # Expose the port Next.js listens on
EXPOSE 3000 EXPOSE 3000
# Health check for container orchestration # Command to start the Next.js server in production mode
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
CMD ["node", "server.js"] CMD ["node", "server.js"]