linux - 以前的 apt-get update & install Docker 层在每次构建期间都被删除

标签 linux ubuntu docker dockerfile apt-get

我有一个包含以下内容的 Dockerfile:

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.

关于linux - 以前的 apt-get update & install Docker 层在每次构建期间都被删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48029756/

相关文章:

ubuntu - 如何使用 apt-get 在 Ubuntu 上安装最新的 uWSGI/stable

升级到 14.04 后 Python 无法正确导入

docker - 为什么 ubuntu 镜像大小比其 iso 文件小得多?

docker - dpkg : docker-ce: dependency problems, 但仍按您的要求删除:nvidia-docker

docker - 在 pod 处触发 kubectl exec 命令后,如何查找因退出代码 137 错误而终止的命令的日志

c - 重新映射堆栈成功,但随后引发 SEGV

linux - 将 VPS 上的 Linux 分区与 CentOS 和 Plesk 合并

c - 在 C 中实现 ls 命令

ruby-on-rails - 部署时的 bundle 程序错误

bash - 如何用sed添加回车?