当前位置:   article > 正文

docker本地镜像元数据解析_简述镜像元数据的作用

简述镜像元数据的作用

docker本地镜像元数据知识点

import hashlib, subprocess
from docker import client

# 定义一个类A,用来获取layer的chainId,使用bottom layer初始化实例
class A(client.Image):
    def __init__(self, diffids):
        self.diffids = diffids
        self.id = diffids[0]

# 定义一个类函数,计算layer的chainId,计算公式为 chainId(layer[n])=sha256sum(chainId(layer[n-1])+" "+diffid(layer[n]))
# bottom layer的chainid就等于diffid
    def f1(self, diffid):
        hashor = hashlib.sha256()
        hashor.update((self.id+" "+diffid).encode())
        self.id = "sha256:" + hashor.hexdigest()
        return self.id

# 定义函数,输入diffids,返回chainids
    def get_chain_ids(self):
        chainIds = [self.diffids[0]]
        for i in range(1, len(self.diffids)):
            chainIds.append(self.f1(diffids[i]))
        return chainIds

# 定义函数,通过tag获取镜像id,并获取镜像元数据
    def get_image_data(self, image):
        result = subprocess.run(['docker', 'images', '-q', '--no-trunc', image],
                                stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        imageid = result.stdout.decode().split()[0].split(':')[1]
        return imageid

# 通过chainId来获取layer的目录位置
    def get_layer(self):
        chainid = self.get_chain_ids()



if __name__ == "__main__":
    diffids = ["sha256:d69483a6face4499acb974449d1303591fcbb5cdce5420f36f8a6607bda11854",
               "sha256:c6e7d78568ef2408bbe07a346468e2d9656611a179fa080800ac95c387cf7cbb",
               "sha256:4acb984b3d93480feb6c0d9c2005e64ae2cf7247ac6b3b1c15e701a73df7f10e",
               "sha256:650c771b4f925a3d4bd3daee034892ddf73e7c88a248428002ae6db2f45faa69",
               "sha256:8e8b8947990a9fc422b8c50bbd0e291441995399cd589f0b2a2cc9501a2c2c9d"]

    a = A(diffids)
    for x in a.get_chain_ids():
        print(x)

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/1016205
推荐阅读
相关标签
  

闽ICP备14008679号