Skip to content

Commit bb82bcf

Browse files
authored
Merge pull request #1626 from datwiz/images-build-error-1625
fix #1625 where ImageCollection.build() could return with incorrect image id
2 parents ce8a6ea + 1223fc1 commit bb82bcf

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

docker/models/images.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,19 +166,20 @@ def build(self, **kwargs):
166166
if isinstance(resp, six.string_types):
167167
return self.get(resp)
168168
last_event = None
169+
image_id = None
169170
for chunk in json_stream(resp):
170171
if 'error' in chunk:
171172
raise BuildError(chunk['error'])
172173
if 'stream' in chunk:
173174
match = re.search(
174-
r'(Successfully built |sha256:)([0-9a-f]+)',
175+
r'(^Successfully built |sha256:)([0-9a-f]+)$',
175176
chunk['stream']
176177
)
177178
if match:
178179
image_id = match.group(2)
179-
return self.get(image_id)
180180
last_event = chunk
181-
181+
if image_id:
182+
return self.get(image_id)
182183
raise BuildError(last_event or 'Unknown')
183184

184185
def get(self, name):

tests/integration/models_images_test.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,17 @@ def test_build_with_multiple_success(self):
3939
self.tmp_imgs.append(image.id)
4040
assert client.containers.run(image) == b"hello world\n"
4141

42+
def test_build_with_success_build_output(self):
43+
client = docker.from_env(version=TEST_API_VERSION)
44+
image = client.images.build(
45+
tag='dup-txt-tag', fileobj=io.BytesIO(
46+
"FROM alpine\n"
47+
"CMD echo Successfully built abcd1234".encode('ascii')
48+
)
49+
)
50+
self.tmp_imgs.append(image.id)
51+
assert client.containers.run(image) == b"Successfully built abcd1234\n"
52+
4253
def test_list(self):
4354
client = docker.from_env(version=TEST_API_VERSION)
4455
image = client.images.pull('alpine:latest')

0 commit comments

Comments
 (0)