The most common command is pip freeze > requirements. txt , which records an environment's current package list into requirements. txt. If you want to install the dependencies in a virtual environment, create and activate that environment first, then use the Install from requirements.
the requirements. txt file is used in the Dockerfile to build the image with the packages. you shouldn't be installing packages in a container manually and saving the image as it's not reproducible. freeze the dependencies to the requirements. txt and in the Dockerfile install dependencies and copy project files.
No. Your requirements file has been pinned to specific versions. If your requirements are set to that version, you should not be trying to upgrade beyond those versions. If you need to upgrade, then you need to switch to unpinned versions in your requirements file.
I'm assuming that at some point in your build process, you're copying your entire application into the Docker image with COPY
or ADD
:
COPY . /opt/app
WORKDIR /opt/app
RUN pip install -r requirements.txt
The problem is that you're invalidating the Docker build cache every time you're copying the entire application into the image. This will also invalidate the cache for all subsequent build steps.
To prevent this, I'd suggest copying only the requirements.txt
file in a separate build step before adding the entire application into the image:
COPY requirements.txt /opt/app/requirements.txt
WORKDIR /opt/app
RUN pip install -r requirements.txt
COPY . /opt/app
# continue as before...
As the requirements file itself probably changes only rarely, you'll be able to use the cached layers up until the point that you add your application code into the image.
This is directly mentioned in Docker's own "Best practices for writing Dockerfiles":
If you have multiple Dockerfile steps that use different files from your context, COPY them individually, rather than all at once. This will ensure that each step’s build cache is only invalidated (forcing the step to be re-run) if the specifically required files change.
For example:
COPY requirements.txt /tmp/ RUN pip install --requirement /tmp/requirements.txt COPY . /tmp/
Results in fewer cache invalidations for the RUN step, than if you put the COPY . /tmp/ before it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With