Minimal Multi-Stage Dockerfiles for Node.js, Python, and Go: Shrink Images by 80%

The Bloated Container Epidemic

It is shockingly common to inspect a company’s production Docker registry and discover 1.8 Gigabyte images for a simple Node.js or Python microservice. These bloated containers ship unnecessary build tools (gcc, make, python headers, npm caches), unneeded operating system utilities (curl, wget, bash), and entire development dependency trees into production.

Bloated containers increase vulnerability exposure (CVEs in unneeded OS packages), inflate cloud registry bandwidth costs, and dramatically slow down Kubernetes autoscaling deployment times.

By implementing Multi-Stage Docker builds and deploying on minimal base images like Alpine or Google’s Distroless, you can shrink images by up to 80% while dramatically improving enterprise security.


Container Size Comparison Benchmark

TechnologyStandard Single-Stage BuildMulti-Stage Distroless / AlpineSize Reduction
Node.js (Express / Nest)1.2 GB (node:22)118 MB (node:22-alpine + prune)90% Smaller
Python (FastAPI / Flask)1.1 GB (python:3.12)95 MB (python:3.12-slim + wheels)91% Smaller
Go (Fiber / Gin)980 MB (golang:1.23)18 MB (scratch / distroless static)98% Smaller

1. Production Multi-Stage Node.js Dockerfile

# -------------------------------------------------------------
# Stage 1: Build & Dependencies
# -------------------------------------------------------------
FROM node:22-alpine AS builder
WORKDIR /app

# Install dependencies needed only for native package compilation
RUN apk add --no-cache python3 make g++

COPY package*.json ./
RUN npm ci

COPY . .
RUN npm run build

# Prune devDependencies to keep only production packages
RUN npm prune --production

# -------------------------------------------------------------
# Stage 2: Minimal Production Runtime
# -------------------------------------------------------------
FROM node:22-alpine AS runner
WORKDIR /app

ENV NODE_ENV=production

# Run as non-root user for security!
USER node

COPY --chown=node:node --from=builder /app/package*.json ./
COPY --chown=node:node --from=builder /app/node_modules ./node_modules
COPY --chown=node:node --from=builder /app/dist ./dist

EXPOSE 3000
CMD ["node", "dist/main.js"]

2. Production Multi-Stage Go Dockerfile (Using Scratch)

# Stage 1: Compiler
FROM golang:1.23-alpine AS builder
WORKDIR /app

RUN apk add --no-cache git ca-certificates
COPY go.mod go.sum ./
RUN go mod download

COPY . .
# Compile standalone statically linked binary without CGO dependencies
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-w -s" -o /app/server .

# Stage 2: True Zero-Byte Scratch Runtime (Final image is ~15 MB!)
FROM scratch
WORKDIR /
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
COPY --from=builder /app/server /server

EXPOSE 8080
ENTRYPOINT ["/server"]

Frequently Asked Questions (FAQ)

What is the difference between Alpine and Distroless?

Alpine is an ultra-minimal Linux distribution (5 MB) that uses musl libc and busybox, providing a lightweight package manager (apk) and basic shell. Distroless (maintained by Google) contains ONLY your application and its runtime dependencies (e.g. Node.js or Java); it contains NO package manager, NO shell, and NO operating system utilities, offering the absolute minimum attack surface.


Continue expanding your technical depth with our related engineering guides:

  • Deploying with Docker Compose: Multi-container orchestration for web applications and databases. Explore the complete guide: Deploying with Docker Compose.
  • Nginx Reverse Proxy & SSL Hardening: Hardening Nginx reverse proxies with TLS 1.3 and security headers. Explore the complete guide: Nginx Reverse Proxy & SSL Hardening.

Published by Mohit Kumar under DevOps & Cloud Infrastructure. Read our deployment guide on The Complete Docker Handbook for Backend Developers.

Authoritative Reference: Consult the Docker Official Multi-Stage Build Guide for normative standards and release specifications.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top