Piece of code with an example of how to build an image (Path to Dockerfile) and push the image to the Registry. The Process has a Log Stream.

This code snippet will be incorporated into the GitLab/Jenkins docker image build and push pipeline.

Code

## pip install docker
## https://docker-py.readthedocs.io/en/stable/images.html

import docker
import json

str_Registry = 'registry.devops-db.internal:5000'
str_Image = 'test_alpine'
str_Version = '1.0.1'
img_Tag = str_Registry + '/' + str_Image + ':' + str_Version

#######################################################################################################################################
### Using the client, it is not possible to stream the log in Build.
###   The only way is to use Low-level API

client = docker.APIClient(base_url='unix://var/run/docker.sock')
for line in client.build(path='/work/docker', rm=True,  tag=str_Image + ':' + str_Version):
    print(json.loads(line))

#######################################################################################################################################
obj_Return = docker.from_env().images.get(str_Image + ':' + str_Version).tag(img_Tag)

for line in client.push(img_Tag, stream=True, decode=True):
    print(line)

obj_Return = client.remove_image(image=img_Tag, force=True)
obj_Return = client.remove_image(image=str_Image + ':' + str_Version, force=True)

{'stream': 'Step 1/2 : FROM alpine'}
{'stream': '\n'}
{'stream': ' ---> ace17d5d883e\n'}
{'stream': 'Step 2/2 : RUN set -e &&   apk update &&   apk add curl ca-certificates --no-cache'}
{'stream': '\n'}
{'stream': ' ---> Running in 48cc44bc1d32\n'}
{'stream': 'fetch https://dl-cdn.alpinelinux.org/alpine/v3.19/main/aarch64/APKINDEX.tar.gz\n'}
{'stream': 'fetch https://dl-cdn.alpinelinux.org/alpine/v3.19/community/aarch64/APKINDEX.tar.gz\n'}
{'stream': 'v3.19.1-266-g4c3242e2631 [https://dl-cdn.alpinelinux.org/alpine/v3.19/main]\nv3.19.1-266-g4c3242e2631 [https://dl-cdn.alpinelinux.org/alpine/v3.19/community]\nOK: 22852 distinct packages available\n'}
{'stream': 'fetch https://dl-cdn.alpinelinux.org/alpine/v3.19/main/aarch64/APKINDEX.tar.gz\n'}
{'stream': 'fetch https://dl-cdn.alpinelinux.org/alpine/v3.19/community/aarch64/APKINDEX.tar.gz\n'}
{'stream': '(1/8) Installing ca-certificates (20240226-r0)\n'}
{'stream': '(2/8) Installing brotli-libs (1.1.0-r1)\n'}
{'stream': '(3/8) Installing c-ares (1.24.0-r1)\n'}
{'stream': '(4/8) Installing libunistring (1.1-r2)\n'}
{'stream': '(5/8) Installing libidn2 (2.3.4-r4)\n'}
{'stream': '(6/8) Installing nghttp2-libs (1.58.0-r0)\n'}
{'stream': '(7/8) Installing libcurl (8.5.0-r0)\n'}
{'stream': '(8/8) Installing curl (8.5.0-r0)\n'}
{'stream': 'Executing busybox-1.36.1-r15.trigger\n'}
{'stream': 'Executing ca-certificates-20240226-r0.trigger\n'}
{'stream': 'OK: 13 MiB in 23 packages\n'}
{'stream': 'Removing intermediate container 48cc44bc1d32\n'}
{'stream': ' ---> df25bc2935ce\n'}
{'aux': {'ID': 'sha256:df25bc2935cee1933f344d7123849a9202cc8b375cb5588264a6ca19e4e95971'}}
{'stream': 'Successfully built df25bc2935ce\n'}
{'stream': 'Successfully tagged test_alpine:1.0.1\n'}
{'status': 'The push refers to repository [registry.devops-db.internal:5000/test_alpine]'}
{'status': 'Preparing', 'progressDetail': {}, 'id': '8c7b9e150fff'}
{'status': 'Preparing', 'progressDetail': {}, 'id': 'b09314aec293'}
{'status': 'Layer already exists', 'progressDetail': {}, 'id': 'b09314aec293'}
{'status': 'Pushing', 'progressDetail': {'current': 70656, 'total': 7018163}, 'progress': '[>                                                  ]  70.66kB/7.018MB', 'id': '8c7b9e150fff'}
{'status': 'Pushing', 'progressDetail': {'current': 341504, 'total': 7018163}, 'progress': '[==>                                                ]  341.5kB/7.018MB', 'id': '8c7b9e150fff'}
{'status': 'Pushing', 'progressDetail': {'current': 2487296, 'total': 7018163}, 'progress': '[=================>                                 ]  2.487MB/7.018MB', 'id': '8c7b9e150fff'}
{'status': 'Pushing', 'progressDetail': {'current': 5735936, 'total': 7018163}, 'progress': '[========================================>          ]  5.736MB/7.018MB', 'id': '8c7b9e150fff'}
{'status': 'Pushing', 'progressDetail': {'current': 7294464, 'total': 7018163}, 'progress': '[==================================================>]  7.294MB', 'id': '8c7b9e150fff'}
{'status': 'Pushed', 'progressDetail': {}, 'id': '8c7b9e150fff'}
{'status': '1.0.1: digest: sha256:ebddcaeefd6eab1cd5949fca0dec76541a9547bc265d763bef8ac777943bb394 size: 739'}
{'progressDetail': {}, 'aux': {'Tag': '1.0.1', 'Digest': 'sha256:ebddcaeefd6eab1cd5949fca0dec76541a9547bc265d763bef8ac777943bb394', 'Size': 739}}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.