Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding build progress callback to ImageCollection.build #3152

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion docker/models/images.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ def reload(self):
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.
Expand Down Expand Up @@ -281,6 +281,9 @@ def build(self, **kwargs):
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
Expand All @@ -305,12 +308,24 @@ def build(self, **kwargs):
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)
Expand Down
18 changes: 18 additions & 0 deletions tests/integration/models_images_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,24 @@ def test_build(self):
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)
Expand Down