Search code examples
dockerdocker-composecookiecutter-django

Pip compiled as part of dockerfile - fastest way to add a new entry to requirements.txt?


I'm using this Dockerfile as part of this docker compose file.

Right now, every time I want to add a new pip requirement, I stop my containers, add the new pip requirement, run docker-compose -f local.yml build, and then restart the containers with docker-compose -f local.yml up. This takes a long time, and it even looks like it's recompiling the container for Postgres if I just add a pip dependency.

What's the fastest way to add a single pip dependency to a container?


Solution

  • This is related to fact that the Docker build cache is being invalidated. When you edit the requirements.txt the step RUN pip install --no-cache-dir -r /requirements/production.txt and all subsequent instructions in the Dockerfile get invalidated. Thus they get re-executed.

    As a best practice, you should avoid invalidaing the build cache as much as possible. This is achieved by moving the steps that change often to the bottom of the Dockerfile. You can edit the Dockerfile and while developing add separate pip installation steps to the end.

    ...
    
    USER django
    
    WORKDIR /app
    
    pip install --no-cache-dir <new package>
    pip install --no-cache-dir <new package2>
    
    ...
    

    And once you are sure of all the dependencies needed, add them to the requirements file. That way you avoid invalidating the build cache early on and only build the steps starting from the installation of the new packages on ward.