1

我正在尝试以实时方式将 docker 图像下载状态发布到前端。目前它只能告诉 docker pull 正在进行。我知道可能有多个不同大小的层,并且尚未开始下载的层的大小可能是未知的。所以我正在考虑显示那里有多少层以及已经开始下载的层的当前下载状态。您知道如何捕获终端控制台上显示的数据docker pull <image>吗?我的程序是用 Python 编写的,所以我想用 python 代码获取数据并将其发送到前端。我检查了 docker 文档,但没有找到任何支持这一点的 api。看起来还没有支持此功能的公共 api。

您还有其他方法可以实现我的目标吗?

欢迎和赞赏任何建议!

谢谢!

4

1 回答 1

0

您需要对此进行低级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"
}
于 2021-01-26T07:19:49.110 回答