首页 > 解决方案 > 如何有条件地拉取 Docker 镜像的最新标签,而不是使用缓存版本?

问题描述

我的 Dockerfile 包含这一行:

COPY --from=whatwg/wattsi:latest /whatwg/wattsi/bin/wattsi /bin/wattsi

whatwg/wattsi即,它正在从Docker Hub 上可用的映像中复制可执行文件。这基本上直接来自多阶段构建的文档。

但是,一旦我运行 Dockerfile,它就会缓存whatwg/wattsi:latest. 然后,任何whatwg/wattsi被推送到 Docker Hub 的后续更新都将被忽略,并使用缓存的副本。(即,这整行被跳过,它创建的层被重用。)

我想要的行为是让 Docker 将远程whatwg/wattsi:latest与本地缓存副本进行比较,如果有差异则重新下载。那可能吗?

我想在不将版本硬编码到我的 Dockerfile 的情况下做到这一点,每次revswhatwg/wattsi都需要更新该版本。whatwg/wattsi

标签: dockerdocker-multi-stage-build

解决方案


无法在 dockerfile 中编写它。

但是你可以运行

docker build --pull

从文档

--pull始终尝试拉取更新版本的映像 https://docs.docker.com/engine/reference/commandline/build/#options

和跑步一样

docker pull whatwg/wattsi:latest

在你的docker build. 它只是检查您的本地映像副本是否是最新的,如果不是,则拉取较新的版本。

这个问题不仅存在于构建,而且存在于运行它们。Kubernetes 通过imagePullPolicy. (见https://kubernetes.io/docs/concepts/containers/images/#updating-images


推荐阅读