Comprehensive guide to containerizing Node.js applications with zero known CVE vulnerabilities using Chainguard Distroless minimal images vs standard Debian/Alpine Docker images.
Comparison • Architecture • Quick Start • Chainguard Distroless Build • Security Best Practices
In modern cloud-native environments, running standard base container images introduces hundreds of unneeded OS packages, package managers (apt, apk), shells (bash, sh), and Common Vulnerabilities and Exposures (CVEs).
This repository provides a direct hands-on comparison between:
- Standard
node:20Image: Familiar, all-inclusive image suitable for development. - Chainguard
cgr.dev/chainguard/node:latestDistroless Image: Minimal, secure, zero-CVE image purpose-built for production environments without unnecessary attack surface.
🎥 Watch the complete step-by-step video breakdown on YouTube.
| Metric | Standard node:20 |
Chainguard Distroless |
|---|---|---|
| Base OS | Debian Linux | Minimal Chainguard / Wolfi |
| Image Size | ~1.1 GB | ~150 MB (~85% smaller) |
| Vulnerability Count (CVEs) | 50+ (High/Medium/Low) | 0 known CVEs |
Shell Included (bash/sh) |
Yes (Expanded attack vector) | No (Distroless) |
| Package Manager Included | apt-get / dpkg |
None |
| User Context | Default root (Unless configured) |
Non-root node:node enforced |
| Recommended Environment | Local Development & Debugging | Production Cloud & Kubernetes |
flowchart TD
subgraph Traditional["Standard Container (node:20)"]
DebianOS["Debian Linux OS (~1GB)"]
DebianPkg["apt, curl, bash, compilers"]
DebianNode["Node.js Runtime"]
AppStandard["Application Code (Runs as Root)"]
DebianOS --- DebianPkg --- DebianNode --- AppStandard
end
subgraph Hardened["Chainguard Distroless (Zero-CVE)"]
WolfiMinimal["Minimal Static OS (~50MB)"]
ZeroTools["No Shell, No Package Manager"]
CGNode["Hardened Node.js Runtime"]
AppSecure["Application Code (Runs as node:node)"]
WolfiMinimal --- ZeroTools --- CGNode --- AppSecure
end
Traditional -->|High Attack Surface| Vulnerabilities["High CVE Risk & Shell Exploits"]
Hardened -->|Minimal Blast Radius| Security["Zero Known CVEs & Hardened Runtime"]
server.js: Minimal HTTP server listening on port3000and servingindex.html.Dockerfile: Standard Node.js 20 multi-purpose Dockerfile.Dockerfile-distroless: Chainguard secure minimal distroless Dockerfile..dockerignore: Excludesnode_modulesand local artifacts from build context.package.json: Node project manifest and execution scripts.
git clone https://github.com/codewithmuh/dockerize-nodejs.git
cd dockerize-nodejs
npm install
npm startVisit http://localhost:3000 to preview the web server.
Build and run using the standard image:
# Build the container image
docker build -t node-server:standard .
# Run the container (maps port 3000)
docker run -it --rm -p 3000:3000 node-server:standardBuild and run using the production-ready Chainguard distroless container:
# Build using the distroless definition
docker build -f Dockerfile-distroless -t node-server:chainguard .
# Run the hardened container
docker run -it --rm -p 3000:3000 node-server:chainguardOpen http://localhost:3000 in your browser.
- Non-Root Execution: Always run Node.js as an unprivileged user (e.g.,
COPY --chown=node:node). - Exclude Package Managers: Never ship
npm,yarn, or OS package managers into production images. - Remove Interactive Shells: Distroless images remove
bashandsh, eliminating arbitrary remote code execution via shell injection. - Sign Images with Cosign: Chainguard images ship with built-in Software Bill of Materials (SBOM) and Sigstore Cosign signatures.
- Continuous Vulnerability Scanning: Scan images using Trivy, Grype, or Docker Scout inside CI/CD pipelines.
Watch the complete walkthrough and vulnerability scanning demo on YouTube: @codewithmuh.
This project is licensed under the MIT License.
