pip package versions are not pinned

Installing Python packages without versions can change the libraries included in a Docker build.

Description

A command such as pip install connexion leaves the package version open to change between builds. This applies to both pip install and pip3 install in shell or JSON exec form.

Potential impact

A change to a direct or transitive dependency during a rebuild can cause runtime errors that did not occur in testing.

Remediation

  • Specify each package as package==version. Options such as --index-url and --trusted-host take their own values and do not pin package versions.
  • Maintain a reviewed requirements file with transitive dependencies pinned. Test the application before updating the pins.

Examples

The versions below illustrate pinning syntax; they are not current security recommendations. Manage the base image and transitive dependencies too, and continue applying security updates.

Before

dockerfile
FROM python:3.12-slim
RUN pip install connexion
RUN pip3 install requests

After

dockerfile
FROM python:3.12-slim
RUN pip install --upgrade pip==24.2 connexion==3.1.0
RUN pip3 install --no-cache-dir requests==2.32.3

Explanation:

  • Before: The versions selected for connexion and requests can change with the repository.
  • After: The commands pin pip, connexion, and requests directly. Their transitive dependencies still need separate pins.

References