在dockerfile中cachingPIP包

我正在尝试为我的python项目设置docker,该项目使用虚拟环境,并在requirements.txt定义了一些依赖关系。

我也设置了Dockerfile docker-compose ,它使用Dockerfile来使用命令build立我的项目图像Dockerfile docker-compose up --build

我的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 

所以,每当我尝试构build映像时,它都会从requirements.txt安装所有的pip模块。

无论如何,我可以cachingpip模块,并在构build映像时使用caching版本。

首先, FROM ubuntu:16.04 dockerfile中的FROM ubuntu:16.04是冗余的,因为单个映像只能有一个上游。

解决问题的简单方法是在添加项目之前将pip命令移至,以便更改项目不会使整个caching失效。

最后,你真的不需要在容器中使用virtualenv,否则你可能做错了什么 。

例如:

 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 . .