Python Docker 如何仅在发生更改时运行 pip requirements.txt?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34398632/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Docker how to run pip requirements.txt only if there was a change?
提问by Prometheus
In a Dockerfile I have a layer which installs requirements.txt
:
在 Dockerfile 中,我有一个安装层requirements.txt
:
FROM python:2.7
RUN pip install -r requirements.txt
When I build the docker image it runs the whole process regardlessof any changes made to this file.
当我构建 docker 映像时,它会运行整个过程,而不管对此文件所做的任何更改。
How do I make sure Docker only runs pip install -r requirements.txt
if there has been a change to the file?
如何确保 Docker 仅pip install -r requirements.txt
在文件发生更改时运行?
Removing intermediate container f98c845d0f05
Step 3 : RUN pip install -r requirements.txt
---> Running in 8ceb63abaef6
Collecting https://github.com/tomchristie/django-rest-framework/archive/master.zip (from -r requirements.txt (line 30))
Downloading https://github.com/tomchristie/django-rest-framework/archive/master.zip
Collecting Django==1.8.7 (from -r requirements.txt (line 1))
采纳答案by helmbert
I'm assuming that at some point in your build process, you're copying your entire application into the Docker image with COPY
or ADD
:
我假设在您的构建过程中的某个时刻,您正在使用COPY
或将整个应用程序复制到 Docker 映像中ADD
:
COPY . /opt/app
WORKDIR /opt/app
RUN pip install -r requirements.txt
The problem is that you're invalidating the Docker build cache every time you're copying the entire application into the image. This will also invalidate the cache for all subsequent build steps.
问题在于,每次将整个应用程序复制到映像中时,都会使 Docker 构建缓存失效。这也将使所有后续构建步骤的缓存无效。
To prevent this, I'd suggest copying only the requirements.txt
file in a separate build step before adding the entire application into the image:
为了防止这种情况,我建议在将整个应用程序添加到图像之前,在单独的构建步骤中仅requirements.txt
复制文件:
COPY requirements.txt /opt/app/requirements.txt
WORKDIR /opt/app
RUN pip install -r requirements.txt
COPY . /opt/app
# continue as before...
As the requirements file itself probably changes only rarely, you'll be able to use the cached layers up until the point that you add your application code into the image.
由于需求文件本身可能很少更改,因此您将能够使用缓存层,直到将应用程序代码添加到图像中为止。
回答by jrc
This is directly mentioned in Docker's own "Best practices for writing Dockerfiles":
这在Docker自己的《编写Dockerfiles的最佳实践》中直接提到:
If you have multiple Dockerfile steps that use different files from your context, COPY them individually, rather than all at once. This will ensure that each step's build cache is only invalidated (forcing the step to be re-run) if the specifically required files change.
For example:
COPY requirements.txt /tmp/ RUN pip install --requirement /tmp/requirements.txt COPY . /tmp/
Results in fewer cache invalidations for the RUN step, than if you put the COPY . /tmp/ before it.
如果您有多个 Dockerfile 步骤使用来自您的上下文的不同文件,请单独复制它们,而不是一次全部复制。这将确保每个步骤的构建缓存仅在特定需要的文件发生更改时才失效(强制重新运行该步骤)。
例如:
COPY requirements.txt /tmp/ RUN pip install --requirement /tmp/requirements.txt COPY . /tmp/
与放置 COPY 相比,RUN 步骤的缓存失效更少。/tmp/ 在它之前。