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

  • Open CV error failed to init raw1394 persisting in docker
  • Docker - Restrictions regarding naming container
  • Docker do not work from build remotely at Jenkins slave. What' the reason?
  • How to copy files from shared directory in multiple Dockerfile?
  • Docker Namespace in kernel level
  • Why do I get “not a tty” when running “tty” in Docker?
  • 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.

  • How to deploy Meteor and Phusion Docker to Digital Ocean with Docker?
  • Ionic in docker: Error: Please install Android target: “android-23”
  • How to initialize and run Docker on windows?
  • Building a Docker file
  • Is Docker or Vagrant a solution to safely “package” a web app?
  • Docker rm does not remove my container if the physical machine dies
  • 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.

    Eg:

    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.