Skip to content

Commit 139850f

Browse files
committed
Merge pull request #698 from docker/jhowardmsft-14530-netmode
`network_mode` now necessary in `host_config`
2 parents 4b33a43 + 1eaf221 commit 139850f

File tree

4 files changed

+39
-21
lines changed

4 files changed

+39
-21
lines changed

docker/client.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -733,7 +733,7 @@ def search(self, term):
733733

734734
@check_resource
735735
def start(self, container, binds=None, port_bindings=None, lxc_conf=None,
736-
publish_all_ports=False, links=None, privileged=False,
736+
publish_all_ports=None, links=None, privileged=None,
737737
dns=None, dns_search=None, volumes_from=None, network_mode=None,
738738
restart_policy=None, cap_add=None, cap_drop=None, devices=None,
739739
extra_hosts=None, read_only=None, pid_mode=None, ipc_mode=None,
@@ -775,7 +775,7 @@ def start(self, container, binds=None, port_bindings=None, lxc_conf=None,
775775
'ulimits is only supported for API version >= 1.18'
776776
)
777777

778-
start_config = utils.create_host_config(
778+
start_config_kwargs = dict(
779779
binds=binds, port_bindings=port_bindings, lxc_conf=lxc_conf,
780780
publish_all_ports=publish_all_ports, links=links, dns=dns,
781781
privileged=privileged, dns_search=dns_search, cap_add=cap_add,
@@ -784,16 +784,18 @@ def start(self, container, binds=None, port_bindings=None, lxc_conf=None,
784784
extra_hosts=extra_hosts, read_only=read_only, pid_mode=pid_mode,
785785
ipc_mode=ipc_mode, security_opt=security_opt, ulimits=ulimits
786786
)
787+
start_config = None
788+
789+
if any(v is not None for v in start_config_kwargs.values()):
790+
if utils.compare_version('1.15', self._version) > 0:
791+
warnings.warn(
792+
'Passing host config parameters in start() is deprecated. '
793+
'Please use host_config in create_container instead!',
794+
DeprecationWarning
795+
)
796+
start_config = utils.create_host_config(**start_config_kwargs)
787797

788798
url = self._url("/containers/{0}/start".format(container))
789-
if not start_config:
790-
start_config = None
791-
elif utils.compare_version('1.15', self._version) > 0:
792-
warnings.warn(
793-
'Passing host config parameters in start() is deprecated. '
794-
'Please use host_config in create_container instead!',
795-
DeprecationWarning
796-
)
797799
res = self._post_json(url, data=start_config)
798800
self._raise_for_status(res)
799801

docker/utils/utils.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,8 @@ def create_host_config(
428428

429429
if network_mode:
430430
host_config['NetworkMode'] = network_mode
431+
elif network_mode is None:
432+
host_config['NetworkMode'] = 'default'
431433

432434
if restart_policy:
433435
host_config['RestartPolicy'] = restart_policy

tests/integration_test.py

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,9 @@ def runTest(self):
181181
container = self.client.create_container(
182182
'busybox',
183183
['ls', mount_dest], volumes={mount_dest: {}},
184-
host_config=create_host_config(binds=binds)
184+
host_config=create_host_config(
185+
binds=binds, network_mode='none'
186+
)
185187
)
186188
container_id = container['Id']
187189
self.client.start(container_id)
@@ -221,7 +223,9 @@ def runTest(self):
221223
container = self.client.create_container(
222224
'busybox',
223225
['ls', mount_dest], volumes={mount_dest: {}},
224-
host_config=create_host_config(binds=binds)
226+
host_config=create_host_config(
227+
binds=binds, network_mode='none'
228+
)
225229
)
226230
container_id = container['Id']
227231
self.client.start(container_id)
@@ -273,7 +277,9 @@ class TestCreateContainerReadOnlyFs(BaseTestCase):
273277
def runTest(self):
274278
ctnr = self.client.create_container(
275279
'busybox', ['mkdir', '/shrine'],
276-
host_config=create_host_config(read_only=True)
280+
host_config=create_host_config(
281+
read_only=True, network_mode='none'
282+
)
277283
)
278284
self.assertIn('Id', ctnr)
279285
self.tmp_containers.append(ctnr['Id'])
@@ -347,7 +353,9 @@ def runTest(self):
347353
class TestCreateContainerPrivileged(BaseTestCase):
348354
def runTest(self):
349355
res = self.client.create_container(
350-
'busybox', 'true', host_config=create_host_config(privileged=True)
356+
'busybox', 'true', host_config=create_host_config(
357+
privileged=True, network_mode='none'
358+
)
351359
)
352360
self.assertIn('Id', res)
353361
self.tmp_containers.append(res['Id'])
@@ -591,7 +599,9 @@ def runTest(self):
591599

592600
container = self.client.create_container(
593601
'busybox', ['sleep', '60'], ports=list(port_bindings.keys()),
594-
host_config=create_host_config(port_bindings=port_bindings)
602+
host_config=create_host_config(
603+
port_bindings=port_bindings, network_mode='bridge'
604+
)
595605
)
596606
id = container['Id']
597607

@@ -717,7 +727,9 @@ def runTest(self):
717727
)
718728
res2 = self.client.create_container(
719729
'busybox', 'cat', detach=True, stdin_open=True,
720-
host_config=create_host_config(volumes_from=vol_names)
730+
host_config=create_host_config(
731+
volumes_from=vol_names, network_mode='none'
732+
)
721733
)
722734
container3_id = res2['Id']
723735
self.tmp_containers.append(container3_id)
@@ -760,7 +772,8 @@ def runTest(self):
760772

761773
res2 = self.client.create_container(
762774
'busybox', 'env', host_config=create_host_config(
763-
links={link_path1: link_alias1, link_path2: link_alias2}
775+
links={link_path1: link_alias1, link_path2: link_alias2},
776+
network_mode='none'
764777
)
765778
)
766779
container3_id = res2['Id']
@@ -781,7 +794,8 @@ class TestRestartingContainer(BaseTestCase):
781794
def runTest(self):
782795
container = self.client.create_container(
783796
'busybox', ['sleep', '2'], host_config=create_host_config(
784-
restart_policy={"Name": "always", "MaximumRetryCount": 0}
797+
restart_policy={"Name": "always", "MaximumRetryCount": 0},
798+
network_mode='none'
785799
)
786800
)
787801
id = container['Id']
@@ -910,7 +924,7 @@ class TestCreateContainerWithHostPidMode(BaseTestCase):
910924
def runTest(self):
911925
ctnr = self.client.create_container(
912926
'busybox', 'true', host_config=create_host_config(
913-
pid_mode='host'
927+
pid_mode='host', network_mode='none'
914928
)
915929
)
916930
self.assertIn('Id', ctnr)
@@ -945,7 +959,7 @@ def runTest(self):
945959

946960
container2 = self.client.create_container(
947961
'busybox', 'cat', host_config=create_host_config(
948-
links={link_path: link_alias}
962+
links={link_path: link_alias}, network_mode='none'
949963
)
950964
)
951965
container2_id = container2['Id']

tests/utils_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ def test_convert_filters(self):
141141
self.assertEqual(convert_filters(filters), expected)
142142

143143
def test_create_empty_host_config(self):
144-
empty_config = create_host_config()
144+
empty_config = create_host_config(network_mode='')
145145
self.assertEqual(empty_config, {})
146146

147147
def test_create_host_config_dict_ulimit(self):

0 commit comments

Comments
 (0)