Reducing Next.js Docker image size
22:19 09 Mar 2025

I would like to reduce my Next.JS app docker image size.

My the Next.js current size is 844MB with this docker file:

# Base image
FROM node:lts-alpine AS base

WORKDIR /app
COPY package*.json ./
EXPOSE 3000

# Install production dependencies
RUN npm ci --production

# Builder stage
FROM base AS builder

COPY . .
RUN npm run build

# Production stage
FROM base AS production

# Create user and group
RUN addgroup -g 1001 -S nodejs
RUN adduser -S nextuser -u 1001
USER nextuser

# Copy only necessary files
COPY --from=builder /app/.next/standalone ./
COPY --from=builder /app/.next/static ./.next/static
COPY --from=builder /app/public ./public
COPY --from=builder /app/package.json ./package.json

# Switch user
USER nextuser

# Start the application
CMD ["node", "server.js"]

# Development stage
FROM node:alpine AS dev
ENV NODE_ENV=development
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
CMD ["npm", "run", "dev"]

here is the history as shown enter image description here

Is it possible to further reduce the docker image size? Thanks

docker