This commit is contained in:
axel7083 2025-01-23 10:19:21 +00:00 committed by GitHub
commit a058eabb36
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 34 additions and 1 deletions

View File

@ -217,7 +217,7 @@ class RegistryData(Model):
class ImageCollection(Collection):
model = Image
def build(self, **kwargs):
def build(self, progress_callback=None, **kwargs):
"""
Build an image and return it. Similar to the ``docker build``
command. Either ``path`` or ``fileobj`` must be set.
@ -279,6 +279,9 @@ class ImageCollection(Collection):
configuration file (``~/.docker/config.json`` by default)
contains a proxy configuration, the corresponding environment
variables will be set in the container being built.
progress_callback Callable[[int, int, str], None]: Will get called
with the following arguments: current_step, max_step, details
Default: `None`.
Returns:
(tuple): The first item is the :py:class:`Image` object for the
@ -303,12 +306,24 @@ class ImageCollection(Collection):
if 'error' in chunk:
raise BuildError(chunk['error'], result_stream)
if 'stream' in chunk:
match = re.search(
r'(^Successfully built |sha256:)([0-9a-f]+)$',
chunk['stream']
)
if match:
image_id = match.group(2)
else:
match = re.match(
r'Step (\d+)/(\d+) : (.+)',
chunk['stream']
)
if match and callable(progress_callback):
progress_callback(
int(match.group(1)),
int(match.group(2)),
match.group(3)
)
last_event = chunk
if image_id:
return (self.get(image_id), result_stream)

View File

@ -20,6 +20,24 @@ class ImageCollectionTest(BaseIntegrationTest):
self.tmp_imgs.append(image.id)
assert client.containers.run(image) == b"hello world\n"
def test_build_with_progress_callback(self):
client = docker.from_env(version=TEST_API_VERSION)
def progress_callback(current_step, max_step, details):
assert max_step == 3
if current_step == 1:
assert details == "FROM alpine"
elif current_step == 2:
assert details == "RUN echo 1"
elif current_step == 3:
assert details == "RUN echo 2"
image, _ = client.images.build(fileobj=io.BytesIO(
b"FROM alpine\n"
b"RUN echo 1\n"
b"RUN echo 2"
), progress_callback=progress_callback)
# @pytest.mark.xfail(reason='Engine 1.13 responds with status 500')
def test_build_with_error(self):
client = docker.from_env(version=TEST_API_VERSION)