Skip to content

Commit a453aef

Browse files
committed
Merge branch 'aaronthebaron-1577-multi-success-message'
2 parents 36b2d81 + 431f7c6 commit a453aef

File tree

2 files changed

+24
-12
lines changed

2 files changed

+24
-12
lines changed

docker/models/images.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -166,18 +166,21 @@ def build(self, **kwargs):
166166
resp = self.client.api.build(**kwargs)
167167
if isinstance(resp, six.string_types):
168168
return self.get(resp)
169-
events = list(json_stream(resp))
170-
if not events:
171-
return BuildError('Unknown')
172-
event = events[-1]
173-
if 'stream' in event:
174-
match = re.search(r'(Successfully built |sha256:)([0-9a-f]+)',
175-
event.get('stream', ''))
176-
if match:
177-
image_id = match.group(2)
178-
return self.get(image_id)
179-
180-
raise BuildError(event.get('error') or event)
169+
last_event = None
170+
for chunk in json_stream(resp):
171+
if 'error' in chunk:
172+
raise BuildError(chunk['error'])
173+
if 'stream' in chunk:
174+
match = re.search(
175+
r'(Successfully built |sha256:)([0-9a-f]+)',
176+
chunk['stream']
177+
)
178+
if match:
179+
image_id = match.group(2)
180+
return self.get(image_id)
181+
last_event = chunk
182+
183+
raise BuildError(last_event or 'Unknown')
181184

182185
def get(self, name):
183186
"""

tests/integration/models_images_test.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,15 @@ def test_build_with_error(self):
2828
assert str(cm.exception) == ("Unknown instruction: "
2929
"NOTADOCKERFILECOMMAND")
3030

31+
def test_build_with_multiple_success(self):
32+
client = docker.from_env(version=TEST_API_VERSION)
33+
image = client.images.build(tag='some-tag', fileobj=io.BytesIO(
34+
"FROM alpine\n"
35+
"CMD echo hello world".encode('ascii')
36+
))
37+
self.tmp_imgs.append(image.id)
38+
assert client.containers.run(image) == b"hello world\n"
39+
3140
def test_list(self):
3241
client = docker.from_env(version=TEST_API_VERSION)
3342
image = client.images.pull('alpine:latest')

0 commit comments

Comments
 (0)