1
1
import json
2
2
import logging
3
3
import os
4
- import re
5
4
6
5
from .. import auth
7
6
from .. import constants
14
13
15
14
class BuildApiMixin (object ):
16
15
def build (self , path = None , tag = None , quiet = False , fileobj = None ,
17
- nocache = False , rm = False , stream = False , timeout = None ,
16
+ nocache = False , rm = False , timeout = None ,
18
17
custom_context = False , encoding = None , pull = False ,
19
18
forcerm = False , dockerfile = None , container_limits = None ,
20
19
decode = False , buildargs = None , gzip = False , shmsize = None ,
21
20
labels = None , cache_from = None , target = None , network_mode = None ,
22
- squash = None , extra_hosts = None ):
21
+ squash = None , extra_hosts = None , platform = None ):
23
22
"""
24
23
Similar to the ``docker build`` command. Either ``path`` or ``fileobj``
25
24
needs to be set. ``path`` can be a local path (to a directory
@@ -67,9 +66,6 @@ def build(self, path=None, tag=None, quiet=False, fileobj=None,
67
66
rm (bool): Remove intermediate containers. The ``docker build``
68
67
command now defaults to ``--rm=true``, but we have kept the old
69
68
default of `False` to preserve backward compatibility
70
- stream (bool): *Deprecated for API version > 1.8 (always True)*.
71
- Return a blocking generator you can iterate over to retrieve
72
- build output as it happens
73
69
timeout (int): HTTP timeout
74
70
custom_context (bool): Optional if using ``fileobj``
75
71
encoding (str): The encoding for a stream. Set to ``gzip`` for
@@ -103,6 +99,7 @@ def build(self, path=None, tag=None, quiet=False, fileobj=None,
103
99
single layer.
104
100
extra_hosts (dict): Extra hosts to add to /etc/hosts in building
105
101
containers, as a mapping of hostname to IP address.
102
+ platform (str): Platform in the format ``os[/arch[/variant]]``
106
103
107
104
Returns:
108
105
A generator for the build output.
@@ -145,23 +142,14 @@ def build(self, path=None, tag=None, quiet=False, fileobj=None,
145
142
exclude = None
146
143
if os .path .exists (dockerignore ):
147
144
with open (dockerignore , 'r' ) as f :
148
- exclude = list (filter (bool , f .read ().splitlines ()))
145
+ exclude = list (filter (
146
+ bool , [l .strip () for l in f .read ().splitlines ()]
147
+ ))
149
148
context = utils .tar (
150
149
path , exclude = exclude , dockerfile = dockerfile , gzip = gzip
151
150
)
152
151
encoding = 'gzip' if gzip else encoding
153
152
154
- if utils .compare_version ('1.8' , self ._version ) >= 0 :
155
- stream = True
156
-
157
- if dockerfile and utils .compare_version ('1.17' , self ._version ) < 0 :
158
- raise errors .InvalidVersion (
159
- 'dockerfile was only introduced in API version 1.17'
160
- )
161
-
162
- if utils .compare_version ('1.19' , self ._version ) < 0 :
163
- pull = 1 if pull else 0
164
-
165
153
u = self ._url ('/build' )
166
154
params = {
167
155
't' : tag ,
@@ -176,12 +164,7 @@ def build(self, path=None, tag=None, quiet=False, fileobj=None,
176
164
params .update (container_limits )
177
165
178
166
if buildargs :
179
- if utils .version_gte (self ._version , '1.21' ):
180
- params .update ({'buildargs' : json .dumps (buildargs )})
181
- else :
182
- raise errors .InvalidVersion (
183
- 'buildargs was only introduced in API version 1.21'
184
- )
167
+ params .update ({'buildargs' : json .dumps (buildargs )})
185
168
186
169
if shmsize :
187
170
if utils .version_gte (self ._version , '1.22' ):
@@ -241,35 +224,33 @@ def build(self, path=None, tag=None, quiet=False, fileobj=None,
241
224
extra_hosts = utils .format_extra_hosts (extra_hosts )
242
225
params .update ({'extrahosts' : extra_hosts })
243
226
227
+ if platform is not None :
228
+ if utils .version_lt (self ._version , '1.32' ):
229
+ raise errors .InvalidVersion (
230
+ 'platform was only introduced in API version 1.32'
231
+ )
232
+ params ['platform' ] = platform
233
+
244
234
if context is not None :
245
235
headers = {'Content-Type' : 'application/tar' }
246
236
if encoding :
247
237
headers ['Content-Encoding' ] = encoding
248
238
249
- if utils .compare_version ('1.9' , self ._version ) >= 0 :
250
- self ._set_auth_headers (headers )
239
+ self ._set_auth_headers (headers )
251
240
252
241
response = self ._post (
253
242
u ,
254
243
data = context ,
255
244
params = params ,
256
245
headers = headers ,
257
- stream = stream ,
246
+ stream = True ,
258
247
timeout = timeout ,
259
248
)
260
249
261
250
if context is not None and not custom_context :
262
251
context .close ()
263
252
264
- if stream :
265
- return self ._stream_helper (response , decode = decode )
266
- else :
267
- output = self ._result (response )
268
- srch = r'Successfully built ([0-9a-f]+)'
269
- match = re .search (srch , output )
270
- if not match :
271
- return None , output
272
- return match .group (1 ), output
253
+ return self ._stream_helper (response , decode = decode )
273
254
274
255
def _set_auth_headers (self , headers ):
275
256
log .debug ('Looking for auth config' )
@@ -290,14 +271,12 @@ def _set_auth_headers(self, headers):
290
271
# Matches CLI behavior: https://github.com/docker/docker/blob/
291
272
# 67b85f9d26f1b0b2b240f2d794748fac0f45243c/cliconfig/
292
273
# credentials/native_store.go#L68-L83
293
- for registry in self ._auth_configs .keys ():
294
- if registry == 'credsStore' or registry == 'HttpHeaders' :
295
- continue
274
+ for registry in self ._auth_configs .get ('auths' , {}).keys ():
296
275
auth_data [registry ] = auth .resolve_authconfig (
297
276
self ._auth_configs , registry
298
277
)
299
278
else :
300
- auth_data = self ._auth_configs .copy ()
279
+ auth_data = self ._auth_configs .get ( 'auths' , {}). copy ()
301
280
# See https://github.com/docker/docker-py/issues/1683
302
281
if auth .INDEX_NAME in auth_data :
303
282
auth_data [auth .INDEX_URL ] = auth_data [auth .INDEX_NAME ]
@@ -308,13 +287,8 @@ def _set_auth_headers(self, headers):
308
287
)
309
288
)
310
289
311
- if utils .compare_version ('1.19' , self ._version ) >= 0 :
312
- headers ['X-Registry-Config' ] = auth .encode_header (
313
- auth_data
314
- )
315
- else :
316
- headers ['X-Registry-Config' ] = auth .encode_header ({
317
- 'configs' : auth_data
318
- })
290
+ headers ['X-Registry-Config' ] = auth .encode_header (
291
+ auth_data
292
+ )
319
293
else :
320
294
log .debug ('No auth config found' )
0 commit comments