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 container for development
  • Mounting directory in docker container and running Gradle command
  • Setting Docker Variables
  • Remote debugging path mapping
  • docker: structure needs cleaning
  • Start tor and polipo when I launch my container
  • 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.

  • Can Ansible deploy Docker containers remotely?
  • Julia cluster using docker
  • How can I tag a Docker image as I'm building it with stack?
  • docker login in shell script dont work
  • Docker Postgress Clear all Data
  • Can I use docker to isolate my development environment?
  • 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.