如何获取Docker Pull状态

ycl3bljg  于 2023-02-03  发布在  Docker
关注(0)|答案(2)|浏览(467)

我尝试发布docker图像下载状态到前端在一个实时的方式。目前它可以告诉docker拉正在进行中。我明白,可能有不同大小的多个层和层的大小还没有开始下载可能是未知的。所以我想显示有多少层和层的当前下载状态已经开始下载。你知道当你运行docker pull <image>时如何捕获终端控制台上显示的数据吗?我的程序是Python的,所以我想用Python代码获取数据并将其发送到前端。我检查了Docker文档,但没有找到任何支持此功能的api。看起来还没有支持此功能的公共api。
你有没有其他方法可以实现我的目标?
欢迎您提出任何建议!
谢谢大家!

8xiog9wr

8xiog9wr1#

为此,您需要一个低级别的pull()调用(docs)。

>>> import docker
>>> import json
>>> client = docker.APIClient(base_url='unix://var/run/docker.sock')
>>> for line in client.pull('busybox', stream=True, decode=True):
...     print(json.dumps(line, indent=4))
{
    "status": "Pulling from library/busybox",
    "id": "latest"
}
{
    "status": "Pulling fs layer",
    "progressDetail": {},
    "id": "e5d9363303dd"
}
{
    "status": "Downloading",
    "progressDetail": {
        "current": 8635,
        "total": 764663
    },
    "progress": "[>                                                  ]  8.635kB/764.7kB",
    "id": "e5d9363303dd"
}
{
    "status": "Downloading",
    "progressDetail": {
        "current": 764663,
        "total": 764663
    },
    "progress": "[==================================================>]  764.7kB/764.7kB",
    "id": "e5d9363303dd"
}
{
    "status": "Verifying Checksum",
    "progressDetail": {},
    "id": "e5d9363303dd"
}
{
    "status": "Download complete",
    "progressDetail": {},
    "id": "e5d9363303dd"
}
{
    "status": "Extracting",
    "progressDetail": {
        "current": 32768,
        "total": 764663
    },
    "progress": "[==>                                                ]  32.77kB/764.7kB",
    "id": "e5d9363303dd"
}
{
    "status": "Extracting",
    "progressDetail": {
        "current": 764663,
        "total": 764663
    },
    "progress": "[==================================================>]  764.7kB/764.7kB",
    "id": "e5d9363303dd"
}
{
    "status": "Extracting",
    "progressDetail": {
        "current": 764663,
        "total": 764663
    },
    "progress": "[==================================================>]  764.7kB/764.7kB",
    "id": "e5d9363303dd"
}
{
    "status": "Pull complete",
    "progressDetail": {},
    "id": "e5d9363303dd"
}
{
    "status": "Digest: sha256:c5439d7db88ab5423999530349d327b04279ad3161d7596d2126dfb5b02bfd1f"
}
{
    "status": "Status: Downloaded newer image for busybox:latest"
}
ht4b089n

ht4b089n2#

docker-py提供了低级API来提取图像沿着流状态。您可以使用此代码段将其显示为类似于docker pull命令的进度条。它首先显示用于显示下载状态的红色进度条,然后显示用于提取的绿色状态条:-)

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)

Progress bar for docker pull through Python API

相关问题