mirror of https://github.com/docker/docker-py.git
Merge 90ddac7b8b
into db7f8b8bb6
This commit is contained in:
commit
a058eabb36
|
@ -217,7 +217,7 @@ class RegistryData(Model):
|
||||||
class ImageCollection(Collection):
|
class ImageCollection(Collection):
|
||||||
model = Image
|
model = Image
|
||||||
|
|
||||||
def build(self, **kwargs):
|
def build(self, progress_callback=None, **kwargs):
|
||||||
"""
|
"""
|
||||||
Build an image and return it. Similar to the ``docker build``
|
Build an image and return it. Similar to the ``docker build``
|
||||||
command. Either ``path`` or ``fileobj`` must be set.
|
command. Either ``path`` or ``fileobj`` must be set.
|
||||||
|
@ -279,6 +279,9 @@ class ImageCollection(Collection):
|
||||||
configuration file (``~/.docker/config.json`` by default)
|
configuration file (``~/.docker/config.json`` by default)
|
||||||
contains a proxy configuration, the corresponding environment
|
contains a proxy configuration, the corresponding environment
|
||||||
variables will be set in the container being built.
|
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:
|
Returns:
|
||||||
(tuple): The first item is the :py:class:`Image` object for the
|
(tuple): The first item is the :py:class:`Image` object for the
|
||||||
|
@ -303,12 +306,24 @@ class ImageCollection(Collection):
|
||||||
if 'error' in chunk:
|
if 'error' in chunk:
|
||||||
raise BuildError(chunk['error'], result_stream)
|
raise BuildError(chunk['error'], result_stream)
|
||||||
if 'stream' in chunk:
|
if 'stream' in chunk:
|
||||||
|
|
||||||
match = re.search(
|
match = re.search(
|
||||||
r'(^Successfully built |sha256:)([0-9a-f]+)$',
|
r'(^Successfully built |sha256:)([0-9a-f]+)$',
|
||||||
chunk['stream']
|
chunk['stream']
|
||||||
)
|
)
|
||||||
if match:
|
if match:
|
||||||
image_id = match.group(2)
|
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
|
last_event = chunk
|
||||||
if image_id:
|
if image_id:
|
||||||
return (self.get(image_id), result_stream)
|
return (self.get(image_id), result_stream)
|
||||||
|
|
|
@ -20,6 +20,24 @@ class ImageCollectionTest(BaseIntegrationTest):
|
||||||
self.tmp_imgs.append(image.id)
|
self.tmp_imgs.append(image.id)
|
||||||
assert client.containers.run(image) == b"hello world\n"
|
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')
|
# @pytest.mark.xfail(reason='Engine 1.13 responds with status 500')
|
||||||
def test_build_with_error(self):
|
def test_build_with_error(self):
|
||||||
client = docker.from_env(version=TEST_API_VERSION)
|
client = docker.from_env(version=TEST_API_VERSION)
|
||||||
|
|
Loading…
Reference in New Issue