Skip to content

Commit

Permalink
Merge pull request docker#1658 from docker/1397-build-network
Browse files Browse the repository at this point in the history
Add network_mode support to Client.build
  • Loading branch information
shin- authored Jun 20, 2017
2 parents a962578 + 39bb78a commit a3b1059
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 10 deletions.
22 changes: 16 additions & 6 deletions docker/api/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def build(self, path=None, tag=None, quiet=False, fileobj=None,
custom_context=False, encoding=None, pull=False,
forcerm=False, dockerfile=None, container_limits=None,
decode=False, buildargs=None, gzip=False, shmsize=None,
labels=None, cache_from=None, target=None):
labels=None, cache_from=None, target=None, network_mode=None):
"""
Similar to the ``docker build`` command. Either ``path`` or ``fileobj``
needs to be set. ``path`` can be a local path (to a directory
Expand Down Expand Up @@ -88,14 +88,16 @@ def build(self, path=None, tag=None, quiet=False, fileobj=None,
- cpusetcpus (str): CPUs in which to allow execution, e.g.,
``"0-3"``, ``"0,1"``
decode (bool): If set to ``True``, the returned stream will be
decoded into dicts on the fly. Default ``False``.
decoded into dicts on the fly. Default ``False``
shmsize (int): Size of `/dev/shm` in bytes. The size must be
greater than 0. If omitted the system uses 64MB.
labels (dict): A dictionary of labels to set on the image.
greater than 0. If omitted the system uses 64MB
labels (dict): A dictionary of labels to set on the image
cache_from (list): A list of images used for build cache
resolution.
resolution
target (str): Name of the build-stage to build in a multi-stage
Dockerfile.
Dockerfile
network_mode (str): networking mode for the run commands during
build
Returns:
A generator for the build output.
Expand Down Expand Up @@ -208,6 +210,14 @@ def build(self, path=None, tag=None, quiet=False, fileobj=None,
'target was only introduced in API version 1.29'
)

if network_mode:
if utils.version_gte(self._version, '1.25'):
params.update({'networkmode': network_mode})
else:
raise errors.InvalidVersion(
'network_mode was only introduced in API version 1.25'
)

if context is not None:
headers = {'Content-Type': 'application/tar'}
if encoding:
Expand Down
11 changes: 7 additions & 4 deletions docker/models/images.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,15 @@ def build(self, **kwargs):
- cpushares (int): CPU shares (relative weight)
- cpusetcpus (str): CPUs in which to allow execution, e.g.,
``"0-3"``, ``"0,1"``
decode (bool): If set to ``True``, the returned stream will be
decoded into dicts on the fly. Default ``False``.
shmsize (int): Size of `/dev/shm` in bytes. The size must be
greater than 0. If omitted the system uses 64MB
labels (dict): A dictionary of labels to set on the image
cache_from (list): A list of images used for build cache
resolution.
resolution
target (str): Name of the build-stage to build in a multi-stage
Dockerfile.
Dockerfile
network_mode (str): networking mode for the run commands during
build
Returns:
(:py:class:`Image`): The built image.
Expand Down
33 changes: 33 additions & 0 deletions tests/integration/api_build_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from docker import errors

import pytest
import six

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

@requires_api_version('1.25')
def test_build_with_network_mode(self):
script = io.BytesIO('\n'.join([
'FROM busybox',
'RUN wget http://google.com'
]).encode('ascii'))

stream = self.client.build(
fileobj=script, network_mode='bridge',
tag='dockerpytest_bridgebuild'
)

self.tmp_imgs.append('dockerpytest_bridgebuild')
for chunk in stream:
pass

assert self.client.inspect_image('dockerpytest_bridgebuild')

script.seek(0)
stream = self.client.build(
fileobj=script, network_mode='none',
tag='dockerpytest_nonebuild', nocache=True, decode=True
)

self.tmp_imgs.append('dockerpytest_nonebuild')
logs = [chunk for chunk in stream]
assert 'errorDetail' in logs[-1]
assert logs[-1]['errorDetail']['code'] == 1

with pytest.raises(errors.NotFound):
self.client.inspect_image('dockerpytest_nonebuild')

def test_build_stderr_data(self):
control_chars = ['\x1b[91m', '\x1b[0m']
snippet = 'Ancient Temple (Mystic Oriental Dream ~ Ancient Temple)'
Expand Down

0 comments on commit a3b1059

Please sign in to comment.