DevOps7 min readJun 04, 2026
Dockerizing Node Pipelines
Learn how to write multi-stage Dockerfiles, minimize container size, cache dependencies, and deploy zero-downtime builds.
Running Node.js apps inside Docker containers ensures execution environment parity across local, staging, and production servers. However, default Docker layouts often result in bloated images containing unnecessary devDependencies.
Multi-Stage Dockerfile
Multi-stage builds separate the compilation stage (requiring Node modules and compilers) from the final runtime image (which only requires production assets). This minimizes the final image footprint.
dockerfileEditor
# Stage 1: Build dependencies
FROM node:20-alpine AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build
# Stage 2: Runtime image
FROM node:20-alpine AS runner
WORKDIR /app
ENV NODE_ENV=production
COPY package*.json ./
RUN npm ci --only=production
COPY --from=builder /app/.next ./.next
COPY --from=builder /app/public ./public
USER node
EXPOSE 3000
CMD ["npm", "start"]Caching & Layers Optimization
- Copy package configurations first: By copying package.json and running 'npm ci' BEFORE copying source files, Docker caches the layer. Source updates won't force a dependency re-download.
- Use Alpine: The alpine base image reduces image size from ~1GB to ~100MB.
- Set non-root user: By executing 'USER node', we protect the host system from container breakout vulnerabilities.
Want to play with this concept?
We build interactive visual terminals for tokenizers, rendering engines, rate limiters, and network topologies. Explore them live!