OCImendby CloudTrace

How to fix CVEs in Python container images

Short answer

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

  1. 01Start from the newest patch tagTags such as python:3.12-slim move to new builds; pin the digest you tested.
  2. 02Upgrade the OS packagesSecurity fixes released since the image was built.
  3. 03Upgrade the Python packagesPin the fixed versions in requirements.txt; pip, setuptools and wheel count too.
  4. 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

Python images in OCImend's reports

Questions

How do I fix CVEs in a Python container image?
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.
Should I use the Alpine or the slim Python image?
Slim (Debian) is the safer default: wheels for musl are rarer, so Alpine builds often compile from source. Both are fine once patched.
Why does my Python image still show CVEs after pip upgrades?
Most are usually in OS packages of the base image, or have no fixed release yet; the report separates both.

Other image types

Node.js · Java · Go · nginx · PostgreSQL · Alpine · Debian · Ubuntu · Red Hat UBI

Fix a Python image now →