A small Flask app, containerised two ways: Dockerfile.weak (deliberately
insecure, kept in the repo for comparison) and Dockerfile (hardened). Every
number below is real scanner output, saved in docs/evidence/
— nothing here is estimated or written to look good.
Dockerfile.weak |
Dockerfile |
|
|---|---|---|
| Base image | python:3.9 (unpinned, full Debian) |
python:3.11-slim, multi-stage |
| Image size | 1.6 GB | 206 MB (87% smaller) |
| Vulnerabilities (Trivy) | 9,314 (232 Critical, 1,840 High) | 183 (3 Critical, 53 High) — 98% fewer |
| Runs as | root | app, uid 100 |
| Secret in image | API_KEY baked into ENV |
none |
| Healthcheck | none | HEALTHCHECK against /health |
Full evidence: trivy-weak-summary.txt,
trivy-hardened-summary.txt,
hadolint-weak.txt,
hadolint-hardened.txt.
The single biggest driver of the vulnerability count on the weak image:
linux-libc-dev alone accounts for 660 Critical/High findings, and a full
ImageMagick install (bundled in the python:3.9 base, several packages,
~60 CVEs each) that this app never touches. Multi-stage + slim removes both —
see docs/decisions.md.
- Multi-stage build. The builder stage installs Python dependencies; the final stage copies only the installed packages across — no compiler, no pip cache, no build tooling in the image that actually runs.
python:3.11-sliminstead ofpython:3.9. Current, and slim strips packages a full image ships that a Flask app never needs.- Non-root user. A dedicated
appuser (uid 100), no login shell, no home directory login access —docker exec ... idconfirms it. - No secret in the image.
API_KEYis gone fromDockerfile; injected at runtime instead (docker run -e API_KEY=...here; a real deployment would use a secrets manager). - Pinned, patched dependency.
flask==2.0.1(CVE-2023-30861) →flask==3.0.3. .dockerignore.COPY . .in the weak version pulls in the whole build context —.git, docs,Dockerfile.weakitself. The hardened version copies onlyapp.py.HEALTHCHECK. Real liveness signal against/health.
The first hardened build crashed on startup:
ModuleNotFoundError: No module named 'flask'. The non-root user had no
$HOME set, so pip install --user's packages weren't found at runtime.
Fixed with ENV HOME=/home/app before USER app. Left in
docs/decisions.md on purpose — locking a container
down for real hits problems a tutorial usually skips.
3 Critical findings remain (perl-base, no upstream fix yet, and unused by
this app) and 2 High findings in build-tool metadata. See
THREAT_MODEL.md for the full breakdown and the accepted
risk. Next step: rebuild from a distroless base to drop perl-base (and
the OS package manager) entirely.
docker build -f Dockerfile.weak -t container-security:weak .
docker build -t container-security:hardened .
trivy image container-security:weak --severity CRITICAL,HIGH
trivy image container-security:hardened --severity CRITICAL,HIGH
hadolint Dockerfile.weak
hadolint Dockerfile
docker run -d -p 8080:8080 container-security:hardened
curl localhost:8080/health.github/workflows/ci.yml runs on every push: Hadolint blocks on real errors,
and Trivy scans the built image and reports High/Critical findings without
blocking yet — it starts gating once the distroless follow-up closes out the
3 remaining Critical findings.