Docker如何仅在发生更改时运行pip requirements.txt?

Docker how to run pip requirements.txt only if there was a change?(Docker如何仅在发生更改时运行pip requirements.txt?)
本文介绍了Docker如何仅在发生更改时运行pip requirements.txt?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

在 Dockerfile 中,我有一个安装 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))

推荐答案

我假设在构建过程中的某个时刻,您将使用 COPY 将整个应用程序复制到 Docker 映像中或 添加:

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 requirements.txt?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

How do I make a list of all members in a discord server using discord.py?(如何使用 discord.py 列出不和谐服务器中的所有成员?)
how to change discord.py bot activity(如何更改 discord.py 机器人活动)
Issues with getting VoiceChannel.members and Guild.members to return a full list(让 VoiceChannel.members 和 Guild.members 返回完整列表的问题)
Add button components to a message (discord.py)(将按钮组件添加到消息(discord.py))
on_message() and @bot.command issue(on_message() 和@bot.command 问题)
How to edit a message in discord.py(如何在 discord.py 中编辑消息)