refactor: simplify Dockerfile for Next.js 14 and optimize build stages

This commit is contained in:
hamed
2025-12-10 15:19:06 +03:30
parent 3daa4cf333
commit defaae05fe
+26 -66
View File
@@ -1,88 +1,48 @@
# ============================================================================= # Dockerfile for Next.js 14 - Optimized for Production
# Multi-stage Dockerfile for Next.js 15 - Production Optimized
# =============================================================================
# Stage 1: Dependencies - Install all dependencies
# Stage 2: Builder - Build the application
# Stage 3: Runner - Production runtime
# =============================================================================
# ----------------------------------------------------------------------------- # Use the official Node.js image with Alpine Linux for a smaller footprint
# Stage 1: Dependencies FROM node:22-alpine AS base
# -----------------------------------------------------------------------------
FROM node:22-alpine AS deps
# Install system dependencies for node-gyp
RUN apk add --no-cache libc6-compat
# Set the working directory in the container
WORKDIR /app WORKDIR /app
# Copy dependency files # Copy package.json and package-lock.json (or yarn.lock)
COPY package.json package-lock.json* ./ COPY package*.json ./
# Install dependencies # Install dependencies - leveraging Docker cache
# Use npm ci for reproducible builds RUN npm i --force
RUN npm ci --legacy-peer-deps || npm install --legacy-peer-deps
# ----------------------------------------------------------------------------- # Copy the rest of the application code
# Stage 2: Builder
# -----------------------------------------------------------------------------
FROM node:22-alpine AS builder
WORKDIR /app
# Copy dependencies from deps stage
COPY --from=deps /app/node_modules ./node_modules
# Copy source code
COPY . . COPY . .
# Set build-time environment variables # Build the Next.js application
ENV NEXT_TELEMETRY_DISABLED=1
ENV NODE_ENV=production
# Build application
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 FROM node:22-alpine AS runner
# Install runtime dependencies # Set the working directory
RUN apk add --no-cache \
libc6-compat \
dumb-init
WORKDIR /app WORKDIR /app
# Set production environment # 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 necessary files from builder (standalone mode) # Copy only the necessary files from the builder stage
COPY --from=builder --chown=nextjs:nodejs /app/next.config.js ./ COPY --from=base /app/next.config.js ./
COPY --from=builder --chown=nextjs:nodejs /app/public ./public COPY --from=base /app/public ./public
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./ COPY --from=base --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static COPY --from=base --chown=nextjs:nodejs /app/.next/static ./.next/static
# Switch to non-root user # Change ownership of all copied files to the non-root user
USER nextjs USER nextjs
# Expose port # Expose the port Next.js listens on
EXPOSE 3000 EXPOSE 3000
# Health check # Command to start the Next.js server in production mode
HEALTHCHECK --interval=30s --timeout=10s --start-period=40s --retries=3 \
CMD node -e "require('http').get('http://localhost:3000/', (res) => { process.exit(res.statusCode === 200 ? 0 : 1); }).on('error', () => { process.exit(1); });"
# Use dumb-init to handle signals properly
ENTRYPOINT ["dumb-init", "--"]
# Start Next.js in standalone mode
CMD ["node", "server.js"] CMD ["node", "server.js"]