From 8f1a82faeb46cf4eeb3c97bf8cc6a7f0a9105e89 Mon Sep 17 00:00:00 2001 From: John Howard Date: Wed, 15 Jul 2015 12:11:54 -0700 Subject: [PATCH 1/6] Add netmode (required by docker) Signed-off-by: John Howard --- docker/utils/utils.py | 10 ++++++++++ tests/integration_test.py | 28 ++++++++++++++++++---------- 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/docker/utils/utils.py b/docker/utils/utils.py index a714c97c4b..396c245931 100644 --- a/docker/utils/utils.py +++ b/docker/utils/utils.py @@ -615,6 +615,16 @@ def create_container_config( if volumes_from is not None: raise errors.InvalidVersion(message.format('volumes_from')) + # NetworkMode must be present and valid in host config from 1.20 onwards + if compare_version('1.20', version) >= 0: + if host_config is None: + host_config = {'NetworkMode': 'default'} + else: + if 'NetworkMode' not in host_config: + host_config['NetworkMode'] = 'default' + elif host_config['NetworkMode'] == '': + host_config['NetworkMode'] = 'default' + return { 'Hostname': hostname, 'Domainname': domainname, diff --git a/tests/integration_test.py b/tests/integration_test.py index 226ea34c2c..49604332db 100644 --- a/tests/integration_test.py +++ b/tests/integration_test.py @@ -181,7 +181,8 @@ def runTest(self): container = self.client.create_container( 'busybox', ['ls', mount_dest], volumes={mount_dest: {}}, - host_config=create_host_config(binds=binds) + host_config=create_host_config( + binds=binds, network_mode='none') ) container_id = container['Id'] self.client.start(container_id) @@ -221,7 +222,8 @@ def runTest(self): container = self.client.create_container( 'busybox', ['ls', mount_dest], volumes={mount_dest: {}}, - host_config=create_host_config(binds=binds) + host_config=create_host_config( + binds=binds, network_mode='none') ) container_id = container['Id'] self.client.start(container_id) @@ -273,7 +275,8 @@ class TestCreateContainerReadOnlyFs(BaseTestCase): def runTest(self): ctnr = self.client.create_container( 'busybox', ['mkdir', '/shrine'], - host_config=create_host_config(read_only=True) + host_config=create_host_config( + read_only=True, network_mode='none') ) self.assertIn('Id', ctnr) self.tmp_containers.append(ctnr['Id']) @@ -347,7 +350,8 @@ def runTest(self): class TestCreateContainerPrivileged(BaseTestCase): def runTest(self): res = self.client.create_container( - 'busybox', 'true', host_config=create_host_config(privileged=True) + 'busybox', 'true', host_config=create_host_config( + privileged=True, network_mode='none') ) self.assertIn('Id', res) self.tmp_containers.append(res['Id']) @@ -591,7 +595,8 @@ def runTest(self): container = self.client.create_container( 'busybox', ['sleep', '60'], ports=list(port_bindings.keys()), - host_config=create_host_config(port_bindings=port_bindings) + host_config=create_host_config( + port_bindings=port_bindings, network_mode='bridge') ) id = container['Id'] @@ -717,7 +722,8 @@ def runTest(self): ) res2 = self.client.create_container( 'busybox', 'cat', detach=True, stdin_open=True, - host_config=create_host_config(volumes_from=vol_names) + host_config=create_host_config( + volumes_from=vol_names, network_mode='none') ) container3_id = res2['Id'] self.tmp_containers.append(container3_id) @@ -760,7 +766,8 @@ def runTest(self): res2 = self.client.create_container( 'busybox', 'env', host_config=create_host_config( - links={link_path1: link_alias1, link_path2: link_alias2} + links={link_path1: link_alias1, link_path2: link_alias2}, + network_mode='none' ) ) container3_id = res2['Id'] @@ -781,7 +788,8 @@ class TestRestartingContainer(BaseTestCase): def runTest(self): container = self.client.create_container( 'busybox', ['sleep', '2'], host_config=create_host_config( - restart_policy={"Name": "always", "MaximumRetryCount": 0} + restart_policy={"Name": "always", "MaximumRetryCount": 0}, + network_mode='none' ) ) id = container['Id'] @@ -910,7 +918,7 @@ class TestCreateContainerWithHostPidMode(BaseTestCase): def runTest(self): ctnr = self.client.create_container( 'busybox', 'true', host_config=create_host_config( - pid_mode='host' + pid_mode='host', network_mode='none' ) ) self.assertIn('Id', ctnr) @@ -945,7 +953,7 @@ def runTest(self): container2 = self.client.create_container( 'busybox', 'cat', host_config=create_host_config( - links={link_path: link_alias} + links={link_path: link_alias}, network_mode='none' ) ) container2_id = container2['Id'] From 80a97cf2ea39fdf0d4b14a30128aee22ef17432a Mon Sep 17 00:00:00 2001 From: Joffrey F Date: Wed, 29 Jul 2015 12:40:54 -0700 Subject: [PATCH 2/6] development version suffix --- docker/version.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker/version.py b/docker/version.py index bc778b93b8..d0aad76af5 100644 --- a/docker/version.py +++ b/docker/version.py @@ -1,2 +1,2 @@ -version = "1.3.1" +version = "1.4.0-dev" version_info = tuple([int(d) for d in version.split("-")[0].split(".")]) From daea185a2397c3b92c80a6cfe7752a9aff672141 Mon Sep 17 00:00:00 2001 From: Joffrey F Date: Wed, 29 Jul 2015 12:43:39 -0700 Subject: [PATCH 3/6] nit: parenthesis alignment --- tests/integration_test.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/tests/integration_test.py b/tests/integration_test.py index 49604332db..59919dabeb 100644 --- a/tests/integration_test.py +++ b/tests/integration_test.py @@ -182,7 +182,8 @@ def runTest(self): 'busybox', ['ls', mount_dest], volumes={mount_dest: {}}, host_config=create_host_config( - binds=binds, network_mode='none') + binds=binds, network_mode='none' + ) ) container_id = container['Id'] self.client.start(container_id) @@ -223,7 +224,8 @@ def runTest(self): 'busybox', ['ls', mount_dest], volumes={mount_dest: {}}, host_config=create_host_config( - binds=binds, network_mode='none') + binds=binds, network_mode='none' + ) ) container_id = container['Id'] self.client.start(container_id) @@ -276,7 +278,8 @@ def runTest(self): ctnr = self.client.create_container( 'busybox', ['mkdir', '/shrine'], host_config=create_host_config( - read_only=True, network_mode='none') + read_only=True, network_mode='none' + ) ) self.assertIn('Id', ctnr) self.tmp_containers.append(ctnr['Id']) @@ -351,7 +354,8 @@ class TestCreateContainerPrivileged(BaseTestCase): def runTest(self): res = self.client.create_container( 'busybox', 'true', host_config=create_host_config( - privileged=True, network_mode='none') + privileged=True, network_mode='none' + ) ) self.assertIn('Id', res) self.tmp_containers.append(res['Id']) @@ -596,7 +600,8 @@ def runTest(self): container = self.client.create_container( 'busybox', ['sleep', '60'], ports=list(port_bindings.keys()), host_config=create_host_config( - port_bindings=port_bindings, network_mode='bridge') + port_bindings=port_bindings, network_mode='bridge' + ) ) id = container['Id'] @@ -723,7 +728,8 @@ def runTest(self): res2 = self.client.create_container( 'busybox', 'cat', detach=True, stdin_open=True, host_config=create_host_config( - volumes_from=vol_names, network_mode='none') + volumes_from=vol_names, network_mode='none' + ) ) container3_id = res2['Id'] self.tmp_containers.append(container3_id) From e32726e83d1375e2ed64747428ba8ca294b90df8 Mon Sep 17 00:00:00 2001 From: Joffrey F Date: Wed, 29 Jul 2015 12:45:15 -0700 Subject: [PATCH 4/6] Moved network_mode default to create_host_config ; small fix to preserve blank host_config in start if no option is provided --- docker/client.py | 2 +- docker/utils/utils.py | 12 ++---------- 2 files changed, 3 insertions(+), 11 deletions(-) diff --git a/docker/client.py b/docker/client.py index e4712c23c3..4e84dd60e6 100644 --- a/docker/client.py +++ b/docker/client.py @@ -780,7 +780,7 @@ def start(self, container, binds=None, port_bindings=None, lxc_conf=None, publish_all_ports=publish_all_ports, links=links, dns=dns, privileged=privileged, dns_search=dns_search, cap_add=cap_add, cap_drop=cap_drop, volumes_from=volumes_from, devices=devices, - network_mode=network_mode, restart_policy=restart_policy, + network_mode=network_mode or '', restart_policy=restart_policy, extra_hosts=extra_hosts, read_only=read_only, pid_mode=pid_mode, ipc_mode=ipc_mode, security_opt=security_opt, ulimits=ulimits ) diff --git a/docker/utils/utils.py b/docker/utils/utils.py index 396c245931..98d17ef50a 100644 --- a/docker/utils/utils.py +++ b/docker/utils/utils.py @@ -428,6 +428,8 @@ def create_host_config( if network_mode: host_config['NetworkMode'] = network_mode + elif network_mode is None: + host_config['NetworkMode'] = 'default' if restart_policy: host_config['RestartPolicy'] = restart_policy @@ -615,16 +617,6 @@ def create_container_config( if volumes_from is not None: raise errors.InvalidVersion(message.format('volumes_from')) - # NetworkMode must be present and valid in host config from 1.20 onwards - if compare_version('1.20', version) >= 0: - if host_config is None: - host_config = {'NetworkMode': 'default'} - else: - if 'NetworkMode' not in host_config: - host_config['NetworkMode'] = 'default' - elif host_config['NetworkMode'] == '': - host_config['NetworkMode'] = 'default' - return { 'Hostname': hostname, 'Domainname': domainname, From 2b7c31e0f7bc4f868e7ea9cdefd4b7eed6ee7a1a Mon Sep 17 00:00:00 2001 From: Joffrey F Date: Wed, 29 Jul 2015 14:23:10 -0700 Subject: [PATCH 5/6] Fixed empty_host_config test --- tests/utils_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/utils_test.py b/tests/utils_test.py index 1c8729ca00..fd9d7f3fc2 100644 --- a/tests/utils_test.py +++ b/tests/utils_test.py @@ -107,7 +107,7 @@ def test_convert_filters(self): self.assertEqual(convert_filters(filters), expected) def test_create_empty_host_config(self): - empty_config = create_host_config() + empty_config = create_host_config(network_mode='') self.assertEqual(empty_config, {}) def test_create_host_config_dict_ulimit(self): From 1eaf221391fbaf33d855f7821f55dcf5ec8027bc Mon Sep 17 00:00:00 2001 From: Joffrey F Date: Mon, 10 Aug 2015 10:45:26 -0700 Subject: [PATCH 6/6] Explicited start config tricks --- docker/client.py | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/docker/client.py b/docker/client.py index 4e84dd60e6..f79ec7bb09 100644 --- a/docker/client.py +++ b/docker/client.py @@ -733,7 +733,7 @@ def search(self, term): @check_resource def start(self, container, binds=None, port_bindings=None, lxc_conf=None, - publish_all_ports=False, links=None, privileged=False, + publish_all_ports=None, links=None, privileged=None, dns=None, dns_search=None, volumes_from=None, network_mode=None, restart_policy=None, cap_add=None, cap_drop=None, devices=None, extra_hosts=None, read_only=None, pid_mode=None, ipc_mode=None, @@ -775,25 +775,27 @@ def start(self, container, binds=None, port_bindings=None, lxc_conf=None, 'ulimits is only supported for API version >= 1.18' ) - start_config = utils.create_host_config( + start_config_kwargs = dict( binds=binds, port_bindings=port_bindings, lxc_conf=lxc_conf, publish_all_ports=publish_all_ports, links=links, dns=dns, privileged=privileged, dns_search=dns_search, cap_add=cap_add, cap_drop=cap_drop, volumes_from=volumes_from, devices=devices, - network_mode=network_mode or '', restart_policy=restart_policy, + network_mode=network_mode, restart_policy=restart_policy, extra_hosts=extra_hosts, read_only=read_only, pid_mode=pid_mode, ipc_mode=ipc_mode, security_opt=security_opt, ulimits=ulimits ) + start_config = None + + if any(v is not None for v in start_config_kwargs.values()): + if utils.compare_version('1.15', self._version) > 0: + warnings.warn( + 'Passing host config parameters in start() is deprecated. ' + 'Please use host_config in create_container instead!', + DeprecationWarning + ) + start_config = utils.create_host_config(**start_config_kwargs) url = self._url("/containers/{0}/start".format(container)) - if not start_config: - start_config = None - elif utils.compare_version('1.15', self._version) > 0: - warnings.warn( - 'Passing host config parameters in start() is deprecated. ' - 'Please use host_config in create_container instead!', - DeprecationWarning - ) res = self._post_json(url, data=start_config) self._raise_for_status(res)