import docker
from rich.progress import Progress
tasks = {}
# Show task progress (red for download, green for extract)
def show_progress(line, progress):
if line['status'] == 'Downloading':
id = f'[red][Download {line["id"]}]'
elif line['status'] == 'Extracting':
id = f'[green][Extract {line["id"]}]'
else:
# skip other statuses
return
if id not in tasks.keys():
tasks[id] = progress.add_task(f"{id}", total=line['progressDetail']['total'])
else:
progress.update(tasks[id], completed=line['progressDetail']['current'])
def image_pull(image_name):
print(f'Pulling image: {image_name}')
with Progress() as progress:
client = docker.from_env()
resp = client.api.pull(image_name, stream=True, decode=True)
for line in resp:
show_progress(line, progress)
if __name__ == '__main__':
# Pull a large image
IMAGE_NAME = 'bitnami/pytorch'
image_pull(IMAGE_NAME)
1条答案
按热度按时间qvtsj1bj1#
docker-py提供了客户端pull API,它提供了完成计数和总计数。你可以将它们相除以得到你需要的百分比。
例如,以下代码段:
将打印:
对于每条消息,您可以获取current/total以获得所需的功能。
我写了一个程序,有丰富的库来显示进度条。它可以单独打印每一层的进度(包括如果是下载还是提取那一层)。
输出结果如下图所示,下载状态显示为红色,解压状态显示为绿色。
Python program to show docker image pull progress bar