按不可变的摘要拉取镜像
在这一步中,你将学习如何使用不可变的摘要(digest)拉取 Docker 镜像。虽然标签(tag)使用起来很方便,但它们可以被更新以指向不同的镜像版本。而摘要则是特定镜像层配置及其历史记录的唯一标识符。这是一种更可靠的方法,能确保你每次拉取的都是完全相同的镜像。
按摘要拉取镜像的格式是 docker pull <镜像名称>@<摘要>
。
首先,让我们找出我们已经拥有的一个镜像的摘要。我们可以使用 docker images --digests
命令来显示本地镜像的摘要。
docker images --digests
你应该会看到类似下面的输出,其中包含 DIGEST
列:
REPOSITORY TAG DIGEST IMAGE ID CREATED SIZE
ubuntu 20.04 sha256:... ... ...
hello-world latest sha256:f52335ce493f8f15cfcf46725b2909db9087b688aedabbbd2a167ae30b6da6fc ... ... ...
让我们使用 hello-world
镜像的摘要。复制完整的摘要字符串(以 sha256:
开头)。
现在,让我们再次尝试拉取 hello-world
镜像,但这次使用它的摘要。将 <摘要>
替换为你复制的实际摘要。
docker pull hello-world@sha256:f52335ce493f8f15cfcf46725b2909db9087b688aedabbbd2a167ae30b6da6fc
由于你可能已经拥有这个镜像层,Docker 会报告该镜像已是最新版本。
sha256:f52335ce493f8f15cfcf46725b2909db9087b688aedabbbd2a167ae30b6da6fc: Pulling from library/hello-world
Digest: sha256:f52335ce493f8f15cfcf46725b2909db9087b688aedabbbd2a167ae30b6da6fc
Status: Downloaded newer image for hello-world@sha256:f52335ce493f8f15cfcf46725b2909db9087b688aedabbbd2a167ae30b6da6fc
docker.io/library/hello-world@sha256:f52335ce493f8f15cfcf46725b2909db9087b688aedabbbd2a167ae30b6da6fc
在生产环境或自动化脚本中,当你需要确保部署的是特定的、不变的镜像版本时,按摘要拉取镜像特别有用。
你可以再次使用 docker images --digests
命令来验证镜像。你会看到 hello-world
镜像被列出,如果之前你按标签拉取过,可能会同时显示标签和摘要。
docker images --digests