How to fix CVEs in Python container images
CVEs in a Python image come from two places: the operating system packages of the base image (Debian or Alpine) and the Python packages in site-packages, including the pip, setuptools and wheel that ship with the image. Rebuild on the newest patch tag of your Python version, upgrade the OS packages in the final stage, upgrade vulnerable Python packages to their fixed versions, and use the slim variant so the compiler toolchain is not in production.
Fix a Python image now →
free, no sign-up; pre-filled with python:3.12-slim, change it to yours
The steps
- 01Start from the newest patch tagTags such as python:3.12-slim move to new builds; pin the digest you tested.
- 02Upgrade the OS packagesSecurity fixes released since the image was built.
- 03Upgrade the Python packagesPin the fixed versions in requirements.txt; pip, setuptools and wheel count too.
- 04Build in two stagesCompile wheels in a builder stage; the runtime stage gets no gcc, no headers.
By hand
FROM python:3.12-slim RUN apt-get update && apt-get upgrade -y && rm -rf /var/lib/apt/lists/* COPY requirements.txt . RUN pip install --no-cache-dir --upgrade pip setuptools wheel \ && pip install --no-cache-dir -r requirements.txt
Then rescan: the CVEs that remain are either without a released fix or in code the application never loads (the report marks both).
Traps that bring CVEs back
- The full python:3.x image carries the build toolchain and hundreds of extra packages: prefer -slim.
- pip, setuptools and wheel bundled in the image are reported like any other package.
- A requirement without a version floor can resolve to an old, vulnerable release in a cached layer.
Python images in OCImend's reports
| Image | Grade | Known CVEs | |
|---|---|---|---|
| python:3.6.13 | F | 460 exploited | Fix |
| python:3.12-alpine | A | 6 | Fix |
| cgr.dev/chainguard/python:latest | A | 0 | Fix |
Questions
How do I fix CVEs in a Python container image?
Should I use the Alpine or the slim Python image?
Why does my Python image still show CVEs after pip upgrades?
Other image types
Node.js · Java · Go · nginx · PostgreSQL · Alpine · Debian · Ubuntu · Red Hat UBI