Khi triển khai ứng dụng Next.js 15 lên môi trường production với Docker, dung lượng Docker image mặc định thường vượt quá 1GB do bao gồm node_modules và build cache không cần thiết.
Dockerfile (Standalone Production Build)
dockerfile
# Multi-stage build pattern for Next.js
FROM node:20-alpine AS base
FROM base AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build
FROM base AS runner
WORKDIR /app
ENV NODE_ENV=production
COPY --from=builder /app/.next/standalone ./
COPY --from=builder /app/.next/static ./.next/static
COPY --from=builder /app/public ./public
EXPOSE 3000
CMD ["node", "server.js"]Mẹo tối ưu hiệu năng
Kích hoạt thuộc tính output: "standalone" trong file next.config.ts để Next.js tự động gom gọn các dependencies thực sự cần thiết khi chạy server.