@@ -217,7 +217,7 @@ def reload(self):
217
217
class ImageCollection (Collection ):
218
218
model = Image
219
219
220
- def build (self , ** kwargs ):
220
+ def build (self , stream = False , timestamp = False , ** kwargs ):
221
221
"""
222
222
Build an image and return it. Similar to the ``docker build``
223
223
command. Either ``path`` or ``fileobj`` must be set.
@@ -245,6 +245,10 @@ def build(self, **kwargs):
245
245
custom_context (bool): Optional if using ``fileobj``
246
246
encoding (str): The encoding for a stream. Set to ``gzip`` for
247
247
compressing
248
+ stream (bool): Print the build output to stdout and stderr while
249
+ the build runs
250
+ timestamp (bool): Prefix build output stream with a timestamp
251
+ "%Y-%m-%d %H:%M:%S"
248
252
pull (bool): Downloads any updates to the FROM image in Dockerfiles
249
253
forcerm (bool): Always remove intermediate containers, even after
250
254
unsuccessful builds
@@ -300,9 +304,20 @@ def build(self, **kwargs):
300
304
image_id = None
301
305
result_stream , internal_stream = itertools .tee (json_stream (resp ))
302
306
for chunk in internal_stream :
307
+ if timestamp :
308
+ timestamp_str = datetime .datetime .now ().strftime ("%Y-%m-%d %H:%M:%S" )
303
309
if 'error' in chunk :
310
+ if stream :
311
+ if timestamp :
312
+ print (timestamp_str , "- " , end = '' , file = sys .stderr )
313
+ print ("Error:" , chunk ['error' ].strip (), flush = True , file = sys .stderr ))
304
314
raise BuildError (chunk ['error' ], result_stream )
305
315
if 'stream' in chunk :
316
+ if stream :
317
+ for line in chunk ["stream" ].splitlines ():
318
+ if timestamp :
319
+ print (timestamp_str , "- " , end = '' )
320
+ print (line .strip (), flush = True )
306
321
match = re .search (
307
322
r'(^Successfully built |sha256:)([0-9a-f]+)$' ,
308
323
chunk ['stream' ]
0 commit comments