πŸ“‹ Cheat Sheets
Β· 1 min read

The Perfect Dockerfile for Node.js β€” Production Ready, Just Copy It


Most Node.js Dockerfiles are bloated. This one builds a production image under 200MB.

The Dockerfile

# Stage 1: Install dependencies
FROM node:20-alpine AS deps
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci --only=production

# Stage 2: Build
FROM node:20-alpine AS builder
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci
COPY . .
RUN npm run build

# Stage 3: Production
FROM node:20-alpine AS runner
WORKDIR /app

ENV NODE_ENV=production

# Don't run as root
RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 appuser

# Copy only what's needed
COPY --from=deps /app/node_modules ./node_modules
COPY --from=builder /app/dist ./dist
COPY --from=builder /app/package.json ./

USER appuser

EXPOSE 3000

CMD ["node", "dist/index.js"]

Why this is good

  • Multi-stage build β€” dev dependencies don’t end up in the final image
  • Alpine base β€” ~50MB instead of ~350MB for the full Node image
  • Non-root user β€” your app doesn’t run as root (security best practice)
  • Layer caching β€” package.json is copied before source code, so npm ci is cached unless dependencies change
  • Production only β€” npm ci --only=production in the deps stage skips dev dependencies

The .dockerignore

Create .dockerignore next to your Dockerfile:

node_modules
.git
.gitignore
.env*
dist
coverage
.next
*.md
.vscode
.idea

This keeps your build context small and your builds fast.

For Next.js apps

Replace the production stage:

FROM node:20-alpine AS runner
WORKDIR /app

ENV NODE_ENV=production
ENV NEXT_TELEMETRY_DISABLED=1

RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs

COPY --from=builder /app/public ./public
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static

USER nextjs

EXPOSE 3000

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

Make sure output: "standalone" is set in your next.config.js.

Build and run

# Build
docker build -t myapp .

# Run
docker run -p 3000:3000 --env-file .env myapp

# Build with no cache (when things are weird)
docker build --no-cache -t myapp .

That’s a production-grade container in one file.

Related: Docker Cheat Sheet

πŸ“˜