Skip to content

Commit 53582a9

Browse files
committed
Add support for extra_hosts option in build
Signed-off-by: Joffrey F <[email protected]>
1 parent cdf9acb commit 53582a9

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

docker/api/build.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def build(self, path=None, tag=None, quiet=False, fileobj=None,
1919
forcerm=False, dockerfile=None, container_limits=None,
2020
decode=False, buildargs=None, gzip=False, shmsize=None,
2121
labels=None, cache_from=None, target=None, network_mode=None,
22-
squash=None):
22+
squash=None, extra_hosts=None):
2323
"""
2424
Similar to the ``docker build`` command. Either ``path`` or ``fileobj``
2525
needs to be set. ``path`` can be a local path (to a directory
@@ -101,6 +101,8 @@ def build(self, path=None, tag=None, quiet=False, fileobj=None,
101101
build
102102
squash (bool): Squash the resulting images layers into a
103103
single layer.
104+
extra_hosts (dict): Extra hosts to add to /etc/hosts in building
105+
containers, as a mapping of hostname to IP address.
104106
105107
Returns:
106108
A generator for the build output.
@@ -229,6 +231,17 @@ def build(self, path=None, tag=None, quiet=False, fileobj=None,
229231
'squash was only introduced in API version 1.25'
230232
)
231233

234+
if extra_hosts is not None:
235+
if utils.version_lt(self._version, '1.27'):
236+
raise errors.InvalidVersion(
237+
'extra_hosts was only introduced in API version 1.27'
238+
)
239+
240+
encoded_extra_hosts = [
241+
'{}:{}'.format(k, v) for k, v in extra_hosts.items()
242+
]
243+
params.update({'extrahosts': encoded_extra_hosts})
244+
232245
if context is not None:
233246
headers = {'Content-Type': 'application/tar'}
234247
if encoding:

docker/models/images.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,10 @@ def build(self, **kwargs):
153153
Dockerfile
154154
network_mode (str): networking mode for the run commands during
155155
build
156+
squash (bool): Squash the resulting images layers into a
157+
single layer.
158+
extra_hosts (dict): Extra hosts to add to /etc/hosts in building
159+
containers, as a mapping of hostname to IP address.
156160
157161
Returns:
158162
(:py:class:`Image`): The built image.

tests/integration/api_build_test.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,38 @@ def test_build_with_network_mode(self):
244244
with pytest.raises(errors.NotFound):
245245
self.client.inspect_image('dockerpytest_nonebuild')
246246

247+
@requires_api_version('1.27')
248+
def test_build_with_extra_hosts(self):
249+
img_name = 'dockerpytest_extrahost_build'
250+
self.tmp_imgs.append(img_name)
251+
252+
script = io.BytesIO('\n'.join([
253+
'FROM busybox',
254+
'RUN ping -c1 hello.world.test',
255+
'RUN ping -c1 extrahost.local.test',
256+
'RUN cp /etc/hosts /hosts-file'
257+
]).encode('ascii'))
258+
259+
stream = self.client.build(
260+
fileobj=script, tag=img_name,
261+
extra_hosts={
262+
'extrahost.local.test': '127.0.0.1',
263+
'hello.world.test': '8.8.8.8',
264+
}, decode=True
265+
)
266+
for chunk in stream:
267+
if 'errorDetail' in chunk:
268+
pytest.fail(chunk)
269+
270+
assert self.client.inspect_image(img_name)
271+
ctnr = self.run_container(img_name, 'cat /hosts-file')
272+
self.tmp_containers.append(ctnr)
273+
logs = self.client.logs(ctnr)
274+
if six.PY3:
275+
logs = logs.decode('utf-8')
276+
assert '127.0.0.1\textrahost.local.test' in logs
277+
assert '8.8.8.8\thello.world.test' in logs
278+
247279
@requires_experimental(until=None)
248280
@requires_api_version('1.25')
249281
def test_build_squash(self):

0 commit comments

Comments
 (0)