如何有条件地提取 Docker 图像的最新标签,而不是使用缓存版本?

How to conditionally pull the latest tag of a Docker image, instead of using the cached version?

我的 Docker 文件包含这一行:

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

即,它正在从 Docker Hub 上可用的 whatwg/wattsi 映像复制可执行文件。这基本上直接来自 multi-stage builds.

上的文档

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

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

我想在不将 whatwg/wattsi 的版本硬编码到我的 Docker 文件中的情况下执行此操作,每次 whatwg/wattsi 版本都需要更新。

docker build 有一个 --pull 选项将 "always attempt to pull a newer version of the image."

第一次构建(没有缓存)

Step 2/2 : COPY --from=whatwg/wattsi:latest /whatwg/wattsi/bin/wattsi /bin/watt
latest: Pulling from whatwg/wattsi
24f0c933cbef: Pull complete 
69e2f037cdb3: Pull complete 
4f7407c4e0dc: Pull complete 
Digest: sha256:f555e4ff56b88313c7c47ca86b83367f9c1ca93552c477a96b9943e907bb7733
Status: Downloaded newer image for whatwg/wattsi:latest
 ---> 2ca5d7a1e784

第二次构建(使用缓存)

Step 2/2 : COPY --from=whatwg/wattsi:latest /whatwg/wattsi/bin/wattsi /bin/watt
 ---> Using cache
 ---> 2ca5d7a1e784

使用 --pull 的第三次构建(检查更新)

Step 2/2 : COPY --from=whatwg/wattsi:latest /whatwg/wattsi/bin/wattsi /bin/watt
latest: Pulling from whatwg/wattsi
Digest: sha256:f555e4ff56b88313c7c47ca86b83367f9c1ca93552c477a96b9943e907bb7733
Status: Image is up to date for whatwg/wattsi:latest
 ---> 7d3390252ae1

没有办法在 dockerfile 中写入它。

但是你可以运行

docker build --pull

来自文档

--pull Always attempt to pull a newer version of the image https://docs.docker.com/engine/reference/commandline/build/#options

与运行宁

相同
docker pull whatwg/wattsi:latest

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

这个问题不仅存在于构建中,也存在于 运行ning 中。 Kubernetes 使用 imagePullPolicy 解决了这个问题。 (参见 https://kubernetes.io/docs/concepts/containers/images/#updating-images