Docker如何运行pip require.txt只有在有变化的时候? [英] Docker how to run pip requirements.txt only if there was a change?

查看:285
本文介绍了Docker如何运行pip require.txt只有在有变化的时候?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Docker文件中,我有一个图层安装 requirements.txt

In a Dockerfile I have a layer which installs requirements.txt:

FROM python:2.7
RUN pip install -r requirements.txt

当我构建Docker图像,它运行整个过程,无论对此文件进行任何更改。

When I build the docker image it runs the whole process regardless of any changes made to this file.

如何确保Docker只运行 pip install -r requirements.txt 如果文件有变化?

How do I make sure Docker only runs pip install -r requirements.txt if there has been a change to the file?

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


推荐答案

我假设在构建过程的某个时刻,您将整个应用程序复制到/ code>添加:

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 . /opt/app
WORKDIR /opt/app
RUN pip install -r requirements.txt

问题是,您每次将整个应用程序复制到映像中时,无效Docker构建缓存。这也将使所有后续构建步骤的缓存无效。

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.

为了防止这种情况,建议只复制 requirements.txt在将整个应用程序添加到图像之前,单独构建步骤中的 文件:

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:

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.

这篇关于Docker如何运行pip require.txt只有在有变化的时候?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆