Cache PIP packages in dockerfile

I am trying to setup docker for my python project, Which uses virtual environment and has defined some dependencies in requirements.txt.

I have setup docker-compose too which uses Dockerfile to build my project image using command docker-compose up --build

  • docker notary : tls: oversized record received with length 20527
  • Heroku: Dockerized Rails API Deploying
  • Multi Docker container with PHP7 fpm and nginx
  • GitLab CI Runner, how to use volumes or mounts in service containers
  • Run a hadoop cluster on docker containers
  • pytest: environment variable to specify pytest.ini location
  • My Dockerfile:

    FROM ubuntu:16.04
     FROM python:3.5  
    MAINTAINER ****  
    ADD . /core-proejct
    WORKDIR /core-project
    RUN pip3 install virtualenv
    RUN . /bin/activate
    RUN pip install -r requirements.txt

    So, every time when I am trying to build the image its installing all pip modules from requirements.txt.

    Is there anyway by which I can cache the pip modules and use the cached version while building image.

  • Docker: Change word in file at container startup
  • Fail to connect mysql in running wordpress docker
  • Why Jenkins not stored data?
  • Connect Docker Containers with remote debugger
  • How to run Kong API Gateway using docker containers?
  • docker - local DNS
  • One Solution collect form web for “Cache PIP packages in dockerfile”

    First things first, the FROM ubuntu:16.04 in your dockerfile is redundant as a single image can only have one upstream.

    The simple way to solve your problem is move your pip commands to before you add your project, so that changing your project doesn’t invalidate the whole cache.

    Lastly, you really don’t need to use virtualenv in a container, otherwise you may be doing something wrong.


    FROM python:3.5
    # MAINTAINER is deprecated. Use LABEL instead.
    LABEL maintainer "your info here"
    WORKDIR /core-project
    ADD ./requirements.txt .
    RUN pip install -r requirements.txt
    # Add everything else now.
    ADD . .
    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.