Dev.to Security πŸ” Cybersecurity πŸ‘ 0 πŸ“– 2 min read

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

  1. Isolated /opt/venv copy: Build compilers and wheel caches stay discarded in builder.
  2. BuildKit COPY --chown --chmod: Avoids duplicating the entire app layer with RUN chown -R / RUN chmod -R.
  3. Explicit numeric USER 10001:10001: Kubernetes runAsNonRoot: true cannot validate named users (USER appuser) without a numeric UIDβ€”always use numeric UIDs!
  4. Native Python HEALTHCHECK: Zero need to install curl or wget inside 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:

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):

What base image do you prefer in 2026β€”slim, alpine, or distroless? Let me know below!

πŸ“° Read the original article on Dev.to Security

Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.