如何从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文件计算对应值:
- 先解压tar包到临时目录:
mkdir hello-world-tmp && tar -xf hello-world.tar -C hello-world-tmp
- 查看manifest.json文件,找到镜像配置文件的名称:
cat hello-world-tmp/manifest.json | jq '.[0].Config'
- 计算该配置文件的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
相关产品推荐
相关产品推荐

