LiveKit Dockerfile Frontend Tutorial

Next.js Multi-Stage Dockerfile

Create an optimized, production-ready Dockerfile for your Next.js application using multi-stage builds. Learn best practices for creating smaller, more secure, and efficient Docker images.

Multi-Stage Build

Learn how to create efficient Docker images with multi-stage builds that separate dependencies, build and runtime environments.

Interactive Tutorial

Step-by-step guide to create the perfect Dockerfile for your Next.js application with detailed explanations.

Production Optimized

Build smaller, more secure images specifically designed for production Next.js deployments.

Multi-Stage Dockerfile Preview

FROM node:20-slim AS base
WORKDIR /app

# 2. Dependencies layer
FROM base AS deps
COPY package.json package-lock.json ./
RUN npm ci

# 3. Build layer
FROM base AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .
RUN npm run build

# 4. Runtime image
FROM node:20-slim AS runner
WORKDIR /app

ENV NODE_ENV=production
ENV PORT=8080
EXPOSE 8080

# Standalone output only
COPY --from=builder /app/.next/standalone ./
COPY --from=builder /app/.next/static ./.next/static
COPY --from=builder /app/public ./public

# ✅ Add this (reads from root of build context)
COPY .env .env
COPY .env.local .env.local

# Use non-root user (optional security)
RUN addgroup --system --gid 1001 nodejs && \
    adduser --system --uid 1001 nextjs
USER nextjs

CMD ["node", "server.js"]

Ready to optimize your Next.js deployment?