在Docker文件中,我有一个安装require.txt的图层:
FROM python:2.7
RUN pip install -r requirements.txt
无论对该文件进行任何更改,当我构建码头服务器映像时都会运行整个过程.
如果文件有更改,我该如何确保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))
我假设在构建过程的某个时刻,您将使用COPY或ADD将整个应用程序复制到Docker映像中:
原文链接:https://www.f2er.com/docker/437027.htmlCOPY . /opt/app
WORKDIR /opt/app
RUN pip install -r requirements.txt
问题在于,您每次将整个应用程序复制到映像中时,无效Docker构建缓存.这也将使所有后续构建步骤的缓存无效.
为了防止这种情况,我建议在将整个应用程序添加到映像之前,单独构建步骤中只复制require.txt文件:
COPY requirements.txt /opt/app/requirements.txt
WORKDIR /opt/app
RUN pip install -r requirements.txt
COPY . /opt/app
# continue as before...