Next.js multi-stage Docker file
The example Dockerfile on the next.js website didn’t work for me. So after some tweaking, and turning it into a multistage Dockerfile to slim it down, I thought I’d share it in case someone else ran into the same problem.
Deployment | Next.js
Learn how to deploy your Next.js app to production, either managed or self-hosted.
This is a multi-stage Dockerfile split into the following three stages:
- The first stage installs some packages for the Alpine Node image andcreates a working directory for the app. The Node packages are then installed after copying the
- The second stage copies the Node modules from the
depsstage, then copies all files in the directory the Dockerfile is in.
yarn buildis called to build the source code.
- The last stage installs some packages and creates a non-root user to run as, then copies necessary from the
.envfile is copied from the same directory as the Dockerfile. The
yarn servecommand calls the
next start. This starts the application in production mode.
# Install dependencies only when needed
FROM node:14.16.1-alpine AS deps
RUN apk add --no-cache libc6-compat nasm autoconf automake bash libltdl libtool gcc make g++ zlib-dev
COPY package.json yarn.lock ./
RUN yarn install --network-timeout 1000000# ==================================================================
# Rebuild the source code only when needed
FROM node:14.16.1-alpine AS builder
COPY --from=deps /app/node_modules ./node_modules
COPY . .# Next.js collects completely anonymous telemetry data about general usage.
# Learn more here: https://nextjs.org/telemetry
# Uncomment the following line in case you want to disable telemetry during the build.
# ENV NEXT_TELEMETRY_DISABLED 1RUN yarn build# ==================================================================
# Production image, copy all the files and run next
FROM node:14.16.1-alpine AS runner
WORKDIR /appENV NODE_ENV production
# Uncomment the following line in case you want to disable telemetry during runtime.
# ENV NEXT_TELEMETRY_DISABLED 1RUN apk add --no-cache libc6-compat nasm autoconf automake bashRUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs# You only need to copy next.config.js if you are NOT using the default configuration
# COPY --from=builder /app/next.config.js ./
COPY --from=builder /app/public ./public
COPY --from=builder /app/package.json ./package.json
COPY --from=builder /app/.next ./.next
COPY --from=builder /app/node_modules ./node_modulesCOPY .env ./# Automatically leverage output traces to reduce image size
# COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
# COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/staticUSER nextjsEXPOSE 3000ENV PORT 3000# CMD ["node", "server.js"]
CMD ["yarn", "serve"]
Hope you found this useful!