Skip to content

Commit a3b1059

Browse files
authored
Merge pull request #1658 from docker/1397-build-network
Add network_mode support to Client.build
2 parents a962578 + 39bb78a commit a3b1059

File tree

3 files changed

+56
-10
lines changed

3 files changed

+56
-10
lines changed

docker/api/build.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def build(self, path=None, tag=None, quiet=False, fileobj=None,
1818
custom_context=False, encoding=None, pull=False,
1919
forcerm=False, dockerfile=None, container_limits=None,
2020
decode=False, buildargs=None, gzip=False, shmsize=None,
21-
labels=None, cache_from=None, target=None):
21+
labels=None, cache_from=None, target=None, network_mode=None):
2222
"""
2323
Similar to the ``docker build`` command. Either ``path`` or ``fileobj``
2424
needs to be set. ``path`` can be a local path (to a directory
@@ -88,14 +88,16 @@ def build(self, path=None, tag=None, quiet=False, fileobj=None,
8888
- cpusetcpus (str): CPUs in which to allow execution, e.g.,
8989
``"0-3"``, ``"0,1"``
9090
decode (bool): If set to ``True``, the returned stream will be
91-
decoded into dicts on the fly. Default ``False``.
91+
decoded into dicts on the fly. Default ``False``
9292
shmsize (int): Size of `/dev/shm` in bytes. The size must be
93-
greater than 0. If omitted the system uses 64MB.
94-
labels (dict): A dictionary of labels to set on the image.
93+
greater than 0. If omitted the system uses 64MB
94+
labels (dict): A dictionary of labels to set on the image
9595
cache_from (list): A list of images used for build cache
96-
resolution.
96+
resolution
9797
target (str): Name of the build-stage to build in a multi-stage
98-
Dockerfile.
98+
Dockerfile
99+
network_mode (str): networking mode for the run commands during
100+
build
99101
100102
Returns:
101103
A generator for the build output.
@@ -208,6 +210,14 @@ def build(self, path=None, tag=None, quiet=False, fileobj=None,
208210
'target was only introduced in API version 1.29'
209211
)
210212

213+
if network_mode:
214+
if utils.version_gte(self._version, '1.25'):
215+
params.update({'networkmode': network_mode})
216+
else:
217+
raise errors.InvalidVersion(
218+
'network_mode was only introduced in API version 1.25'
219+
)
220+
211221
if context is not None:
212222
headers = {'Content-Type': 'application/tar'}
213223
if encoding:

docker/models/images.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,15 @@ def build(self, **kwargs):
144144
- cpushares (int): CPU shares (relative weight)
145145
- cpusetcpus (str): CPUs in which to allow execution, e.g.,
146146
``"0-3"``, ``"0,1"``
147-
decode (bool): If set to ``True``, the returned stream will be
148-
decoded into dicts on the fly. Default ``False``.
147+
shmsize (int): Size of `/dev/shm` in bytes. The size must be
148+
greater than 0. If omitted the system uses 64MB
149+
labels (dict): A dictionary of labels to set on the image
149150
cache_from (list): A list of images used for build cache
150-
resolution.
151+
resolution
151152
target (str): Name of the build-stage to build in a multi-stage
152-
Dockerfile.
153+
Dockerfile
154+
network_mode (str): networking mode for the run commands during
155+
build
153156
154157
Returns:
155158
(:py:class:`Image`): The built image.

tests/integration/api_build_test.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
from docker import errors
77

8+
import pytest
89
import six
910

1011
from .base import BaseAPIIntegrationTest
@@ -211,6 +212,38 @@ def test_build_container_with_target(self):
211212
info = self.client.inspect_image('build1')
212213
self.assertEqual(info['Config']['OnBuild'], [])
213214

215+
@requires_api_version('1.25')
216+
def test_build_with_network_mode(self):
217+
script = io.BytesIO('\n'.join([
218+
'FROM busybox',
219+
'RUN wget http://google.com'
220+
]).encode('ascii'))
221+
222+
stream = self.client.build(
223+
fileobj=script, network_mode='bridge',
224+
tag='dockerpytest_bridgebuild'
225+
)
226+
227+
self.tmp_imgs.append('dockerpytest_bridgebuild')
228+
for chunk in stream:
229+
pass
230+
231+
assert self.client.inspect_image('dockerpytest_bridgebuild')
232+
233+
script.seek(0)
234+
stream = self.client.build(
235+
fileobj=script, network_mode='none',
236+
tag='dockerpytest_nonebuild', nocache=True, decode=True
237+
)
238+
239+
self.tmp_imgs.append('dockerpytest_nonebuild')
240+
logs = [chunk for chunk in stream]
241+
assert 'errorDetail' in logs[-1]
242+
assert logs[-1]['errorDetail']['code'] == 1
243+
244+
with pytest.raises(errors.NotFound):
245+
self.client.inspect_image('dockerpytest_nonebuild')
246+
214247
def test_build_stderr_data(self):
215248
control_chars = ['\x1b[91m', '\x1b[0m']
216249
snippet = 'Ancient Temple (Mystic Oriental Dream ~ Ancient Temple)'

0 commit comments

Comments
 (0)