简体   繁体   English

无法在 docker build 中继续

[英]not able to continue in docker build

I am trying to build an image using docker but if I made some changes and build it again then it starts from the beginning.我正在尝试使用 docker 构建一个图像,但是如果我做了一些更改并再次构建它,那么它会从头开始。 Excepting it to continue from the line I have made changes.除了它继续从我所做的更改行。

sudo docker build -t flask-app .

I don't know why but it is not using cache我不知道为什么,但它没有使用缓存

Update更新

example Initial dockerfile示例初始 dockerfile

FROM python:3.6

#working dir
WORKDIR /app

#copying content
COPY . /app

#Requirement installation
RUN pip3 install -r requirements.txt

#Command to execute
CMD [ "python3", "app.py" ]

Later dockerfile后来的dockerfile

FROM python:3.6

#working dir
WORKDIR /app

#copying content
COPY . /app

#Requirement installation
RUN pip3 install -r requirements.txt

#Port to expose
EXPOSE 8000

#Command to execute
CMD [ "python3", "app.py" ]

Expecting it know continue from Expose 8000 but it continues from beginning Any suggestions?期待它知道从 Expose 8000 继续,但它从一开始就继续有任何建议吗? Thanks谢谢

The COPY Command#复制命令#

Use COPY . /app使用COPY . /app COPY . /app after requirement installation, because Dockerfile is also included in this and will be copied to /app. COPY . /app需求安装后,因为Dockerfile也包含在这个里面,会复制到/app。

The COPY command in a Dockerfile allows you to import one or more external files into a Docker image. Dockerfile 中的 COPY 命令允许您将一个或多个外部文件导入到 Docker 映像中。 The COPY commands always get executed in order to have the latest version of the external file. COPY 命令总是被执行以获取最新版本的外部文件。

If the contents of all external files on the first COPY command are the same, the layer cache will be used and all subsequent commands until the next ADD or COPY command will use the layer cache.如果第一个 COPY 命令上的所有外部文件的内容都相同,将使用图层缓存,所有后续命令直到下一个 ADD 或 COPY 命令都将使用图层缓存。

However, if the contents of one or more external files are different, then all subsequent commands will be executed without using the layer cache.但是,如果一个或多个外部文件的内容不同,那么所有后续命令都将在不使用图层缓存的情况下执行。

In order to take advantage of Layer Caching in Docker you should structure your Dockerfile in a way that frequently changing steps such as COPY to be located towards the end of the Dockerfile file.为了利用 Docker 中的层缓存,您应该以一种经常更改步骤(例如 COPY)的方式来构建 Dockerfile,以便将其定位到 Dockerfile 文件的末尾。 This will ensure that the steps concerned with doing the same action are not unnecessarily rebuilt.这将确保不会不必要地重新构建与执行相同操作相关的步骤。

For more info https://docs.semaphoreci.com/ci-cd-environment/docker-layer-caching/有关更多信息https://docs.semaphoreci.com/ci-cd-environment/docker-layer-caching/

COPY . /app

If there's any ever so slight change in the context you pass in, then this step will get reevaluated.如果您传入的上下文有任何微小的变化,那么这一步将被重新评估。 It can be worth it to only copy requirements.txt first, then install the dependencies from it, then copy the rest, to use the cache effectively.值得只先复制requirements.txt ,然后从中安装依赖项,然后复制其余部分,以有效地使用缓存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM