docker / docker-py

A Python library for the Docker Engine API
https://docker-py.readthedocs.io/
Apache License 2.0
6.8k stars 1.67k forks source link

How can I parse and handle image push response correctly ? #2113

Open vincenthcui opened 6 years ago

vincenthcui commented 6 years ago

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!

shin- commented 6 years ago

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

vincenthcui commented 6 years ago

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!

astrocaribe commented 5 years ago

@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.

medley56 commented 6 days ago

@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