Multi-stage docker image #2
We have seen alot of multi-stage docker image, however one that perplexing has always been NextJS, they are huge unlike other. Go Language has extremely small footprint, infact one of the very few programming language that can have around 10MB, for NextJS, a basic template can go as huge as 1.5GB
This is a recent build i did for a scenario, a basic template with added user instead of using root. What if I remove the added user, how much can i save?
# Stage 1: Build Stage
FROM node:20-alpine AS build
WORKDIR /app
COPY package*.json ./
RUN npm install --frozen-lockfile --prefer-offline --no-audit
COPY . .
RUN npm run build
# Stage 2: Production Stage
FROM node:20-alpine
# Create a non-root user and group
RUN addgroup -S appgroup && adduser -S appuser -G appgroup
WORKDIR /app
COPY --from=build /app/package.json ./package.json
RUN npm install --production --frozen-lockfile --prefer-offline --no-audit
# Copy built application from the build stage
COPY --from=build /app/.next ./.next
COPY --from=build /app/public ./public
RUN npm prune --production
# Change ownership of the files to the non-root user
RUN chown -R appuser:appgroup /app
# Switch to the non-root user
USER appuser
# Expose the default Next.js port
EXPOSE 3000
# Start the application
CMD ["npm", "start"]
Continue removing the non-root user and group
It saved about 140mb. However looking at NextJS, still high on the usage of image size
I tried some online claim stating using
It ended up even bigger at 2.2GB
Is NextJS tailwind in need of a real look at their architecture as a whole?
Any additional feedback to reduce the image size?