docker-py icon indicating copy to clipboard operation
docker-py copied to clipboard

How can I parse and handle image push response correctly ?

Open vincenthcui opened this issue 7 years ago • 4 comments

I use client.images.push(repository, stream=True) api to push docker image to remote registry, and get response just like

{"status":"Pushing repository yourname/app (1 tags)"}
{"status":"Pushing","progressDetail":{},"id":"511136ea3c5a"}
{"status":"Image already pushed, skipping","progressDetail":{},
 "id":"511136ea3c5a"}

and now I want to parse those json response to know if the push action is success or not, but find no reference in either docker-py docs nor docker api docs.

so, how can I parse and hadle image push response correctly?

Thx!

vincenthcui avatar Aug 13 '18 12:08 vincenthcui

http://docker-py.readthedocs.io/en/stable/images.html#docker.models.images.ImageCollection.push

decode (bool) – Decode the JSON data from the server into dicts. Only applies with stream=True

shin- avatar Aug 13 '18 18:08 shin-

Thanks for quick response. And I think I don't express my meanings clearly :(

What I want is to get meanings from every line of those response, instead of how to format json data. For examples, how many kinds of status need to be process correctly, or how many fields in those lines.

Thank you!

vincenthcui avatar Aug 14 '18 06:08 vincenthcui

@vincenthcui This may be too late for you, but here is an example of how i use the pull statuses:

def extract_progress_info(line):
    """
    Extract progress information from a Docker Pull progress object
    :param line: Docker pull progress object (Json)
    :return: Transoformed progress object (Json)
    """
    # Extract pieces of interest
    pid = line['id'] if 'id' in line else ""
    status = line['status'] if 'status' in line else ""
    progress_detail = line['progressDetail'] if 'progressDetail' in line else {}
    complete_statuses = ['Download complete', 'Pull complete', 'Already exists', 'Layer already exists', 'Pushed']

    percent = 0
    if status in complete_statuses:
        percent = 100
    elif progress_detail != {}:
        current = line['progressDetail']['current']
        total = line['progressDetail']['total']
        percent = round((current / total) * 100)

    # Create progress object
    progress = {'id': pid, 'status': status, 'percent': percent}

    return progress

The input to this function is decoded as @shin- alluded above.

astrocaribe avatar Jan 03 '19 22:01 astrocaribe

@astrocaribe Have you ever noticed that current ends up being a larger value than total? I've been chasing my tail trying to figure out why I'm always ending up at like 102% complete but the docker API docs are not very helpful. https://docs.docker.com/reference/api/engine/version/v1.47/#tag/Image/operation/ImagePush

Example output from my version of your code above:

Status: Pushing, ID: 2b2b3410e106, Progress: 517843456/509753187

medley56 avatar Oct 11 '24 22:10 medley56