How to Shrink Python & Node Docker Images by 85% and Pass Trivy Scans on the First Try (2026 Multi-Stage Guide)
A single naive FROM node:22 or FROM python:3.12 Dockerfile routinely ships a 1.1 GB container image packed with gcc, curl, package-manager caches, root privileges, and 40+ HIGH/CRITICAL CVEs flagged by Trivy in CI. Here
A single naive FROM node:22 or FROM python:3.12 Dockerfile routinely ships a 1.1 GB container image packed with gcc, curl, package-manager caches, root privileges, and 40+ HIGH/CRITICAL CVEs flagged by Trivy in CI.
Here is the exact 2026 hardened multi-stage Dockerfile blueprint that drops image size from ~1.1 GB to <120 MB, eliminates build-tool attack surface, and passes CIS + Trivy scans cleanly.
1. The Hardened Multi-Stage Python (FastAPI) Blueprint
# syntax=docker/dockerfile:1.7
FROM python:3.12-slim AS builder
ENV PIP_NO_CACHE_DIR=1 \
PIP_DISABLE_PIP_VERSION_CHECK=1
WORKDIR /build
COPY requirements.txt .
RUN python -m venv /opt/venv \
&& /opt/venv/bin/pip install --upgrade pip \
&& /opt/venv/bin/pip install -r requirements.txt
# --- Minimal Runtime Stage ---
FROM python:3.12-slim AS runtime
ENV PATH="/opt/venv/bin:$PATH" \
PYTHONDONTWRITEBYTECODE=1 \
PYTHONUNBUFFERED=1
RUN addgroup --gid 10001 appgroup \
&& adduser --uid 10001 --gid 10001 --disabled-password --gecos "" appuser
WORKDIR /app
COPY --from=builder --chown=10001:10001 /opt/venv /opt/venv
COPY --chown=10001:10001 --chmod=644 ./app /app/app
USER 10001:10001
EXPOSE 8000
HEALTHCHECK --interval=30s --timeout=3s --start-period=10s --retries=3 \
CMD python -c "import urllib.request; urllib.request.urlopen('http://127.0.0.1:8000/healthz')" || exit 1
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]
2. Why Every Line Matters
-
Isolated
/opt/venvcopy: Build compilers and wheel caches stay discarded inbuilder. -
BuildKit
COPY --chown --chmod: Avoids duplicating the entire app layer withRUN chown -R/RUN chmod -R. -
Explicit numeric
USER 10001:10001: KubernetesrunAsNonRoot: truecannot validate named users (USER appuser) without a numeric UIDβalways use numeric UIDs! -
Native Python
HEALTHCHECK: Zero need to installcurlorwgetinside the runtime image.
3. Gate Every PR with Trivy in GitHub Actions
Add a 15-line Trivy check right after docker build so CRITICAL CVEs never reach production:
- name: Scan Docker image with Trivy
uses: aquasecurity/[email protected]
with:
image-ref: 'myapp:${{ github.sha }}'
format: 'table'
exit-code: '1'
ignore-unfixed: true
severity: 'CRITICAL,HIGH'
You can inspect a complete working FastAPI + Redis + Terraform EKS + Helm + ArgoCD + Trivy CI reference repo here: cloudpulse-gitops-platform on GitHub.
Free 100% Browser-Local DevOps & Security Tools (Zero Signup, Zero Tracking)
Audit your Dockerfiles, Kubernetes manifests, .env secrets, and AWS VPC CIDRs completely locally in your browser:
- Kubernetes & Dockerfile CIS Security Auditor (12 Checks)
- Linux Chmod & BuildKit
--chmodCalculator - .env β Kubernetes Secret, ConfigMap & AWS ExternalSecret Converter
- AWS VPC & EKS CIDR Subnet Calculator + Terraform HCL
- Cron Expression Explainer & Hardened K8s CronJob Builder
- 100% Local JWT Decoder & Security Auditor
Want the Complete Production Deployment & SRE Starter Pack?
Grab the SaaS Deploy & SRE Starter Pack 2026 (hardened multi-stage Docker + Nginx stack, modular Terraform AWS VPC/EKS + IRSA, Helm charts, encrypted S3/R2 Postgres backups, zero-downtime rollback scripts, Prometheus SLO alerts & Trivy CI/CD) β use launch code PH20 for 20% off ($15.20):
-
Ko-fi (Direct Stripe / Apple Pay): SaaS Deploy & SRE Starter Pack 2026 (
PH20) -
Gumroad: SaaS Deploy & SRE Starter Pack 2026 (
PH20) -
All-Access 5-in-1 Indie Founder Bundle ($19.20 with
PH20): Ko-fi | Gumroad
What base image do you prefer in 2026βslim, alpine, or distroless? Let me know below!
Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes β full credit and traffic to the original publisher.