Freezing PIP versions in a Docker container and creating requirements.txt

Tiempo de lectura: < 1 minuto

We will learn today how to freeze versions and update your container with a fixed requirements.txt.

Book autumn - pexels
docker exec -it nombre_contenedor /bin/bash
  • This leaves you in the terminal inside the container.
  • Make sure to be in the folder where you installed your packages or where your venv is located.

Generate requirements.txt

Inside the container, execute:

pip freeze > requirements.txt
  • This generates a file named ‘requirements.txt’ with the exact versions of all installed libraries.
  • You can copy it outside the container using:
docker cp nombre_contenedor:/app/requirements.txt ./requirements.txt

Change ‘/app/’ to the path inside the container where your project is located.

Instead of running a long list of ‘pip install’ commands, do something like:

COPY requirements.txt . RUN pip install --no-cache-dir -r requirements.txt
  • This freezes versions automatically and makes the image more reproducible.

Rebuild the container

docker-compose up -d --build

<

blockquote class=”wp-block-quote is-layout-flow wp-block-quote-is-layout-flow”>

With this you avoid errors like Pydantic v2 or bcrypt that appear “out of the blue” when rebuilding the image.

Leave a Comment