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

  • chown docker volumes on host (possibly through docker-compose)
  • Oracle DB+Weblogic in a single container
  • Linked docker containers can't communicate with each other
  • What is the name of the Docker daemon?
  • lsmod returns empty output on Jelastic
  • Bluemix Docker Container cf ic login the local Docker daemon is not reachable to authenticate
  • 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 Toolbox Windows Logging into quay.io
  • Docker build on Bluemix Containers can't find Dockerfile
  • Docker nginx redirect HTTP to HTTPS
  • Can Ansible deploy Docker containers remotely?
  • Increase HeapSize in Tomcat for all my applications
  • Proper way to monitor PostgreSQL running inside a docker container
  • 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.