You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从Docker tar归档文件中获取对应镜像的摘要?

问题

我已经执行了以下操作:拉取hello-world镜像,将其保存为hello-world.tar,并通过docker images --digests命令获取了已加载镜像的摘要sha256:faa03e786c97f07ef34423fccceeec2398ec8a5759259f94d99078f264e9d7af。请问如何从hello-world.tar文件中获取该摘要,且使其与上述摘要一致?

已执行操作

docker pull hello-world
docker save hello-world > hello-world.tar

获取已加载镜像摘要的命令

docker images --digests --filter=reference='hello-world'

命令输出结果

REPOSITORY    TAG       DIGEST                                                                    IMAGE ID       CREATED         SIZE
hello-world   latest    sha256:faa03e786c97f07ef34423fccceeec2398ec8a5759259f94d99078f264e9d7af   feb5d9fea6a5   14 months ago   13.3kB

解决方法

方法1:临时加载镜像后获取摘要

直接将tar文件加载到Docker,再用你熟悉的命令查看摘要,结果会完全一致:

# 加载tar文件到Docker
docker load < hello-world.tar
# 查看镜像摘要
docker images --digests --filter=reference='hello-world'

方法2:直接解析tar文件计算摘要(无需加载)

如果不想加载镜像到本地Docker环境,可以手动解析tar文件计算对应值:

  1. 先解压tar包到临时目录:
mkdir hello-world-tmp && tar -xf hello-world.tar -C hello-world-tmp
  1. 查看manifest.json文件,找到镜像配置文件的名称:
cat hello-world-tmp/manifest.json | jq '.[0].Config'
  1. 计算该配置文件的SHA256哈希值,这个值就是镜像的digest:
sha256sum hello-world-tmp/$(cat hello-world-tmp/manifest.json | jq -r '.[0].Config') | awk '{print "sha256:"$1}'

执行后输出的字符串会和你之前获取的摘要完全匹配。

注意:此方法需要安装jq工具用于JSON解析,可通过系统包管理器安装(如apt install jq或yum install jq)。


内容的提问来源于stack exchange,提问作者J'e

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 09:50:17