之前的 apt-get update & install Docker 层在每次构建时被移除

Previous apt-get update & install Docker layer removed during every build

我有一个 Docker 文件包含以下内容:

FROM ubuntu:17.10
WORKDIR /app
ADD . /app
RUN apt-get update && apt-get install -y \
python3-pip \
python3-numpy \
ffmpeg \
python3.6 \
xz-utils
...

每次我 运行 docker build 时,在 RUN 语句中创建的层都会被删除,我不确定为什么会这样。安装所有依赖项需要很长时间,所以我希望 Docker 缓存该层并在将来再次使用它。

我该怎么做才能获得这种行为?

谢谢。

根据documentation,如果一层的缓存失效,则需要重新构建后续层。因此,最好在 Dockerfile 中首先执行所有静态步骤(例如,在您的情况下,您可以向上移动 RUN apt-get ... 命令)。希望清楚

For the ADD and COPY instructions, the contents of the file(s) in the image are examined and a checksum is calculated for each file. The last-modified and last-accessed times of the file(s) are not considered in these checksums. During the cache lookup, the checksum is compared against the checksum in the existing images. If anything has changed in the file(s), such as the contents and metadata, then the cache is invalidated.

Once the cache is invalidated, all subsequent Dockerfile commands will generate new images and the cache will not be used.