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)