From ef452ed4c1161a0b71369be7c86e450fd41b52f8 Mon Sep 17 00:00:00 2001 From: smothiki Date: Fri, 24 Apr 2015 13:45:20 -0600 Subject: [PATCH 1/3] make memory units compatible with native docker cli Signed-off-by: sivaram mothiki --- docker/utils/utils.py | 3 +++ tests/utils_test.py | 8 +++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/docker/utils/utils.py b/docker/utils/utils.py index a18939de..96b3e764 100644 --- a/docker/utils/utils.py +++ b/docker/utils/utils.py @@ -322,6 +322,9 @@ def parse_bytes(s): if len(s) == 0: s = 0 else: + if s[-2:-1].isalpha() and s[-1].isalpha(): + if (s[-1] == "b" or s[-1] == "B"): + s = s[:-1] units = BYTE_UNITS suffix = s[-1].lower() diff --git a/tests/utils_test.py b/tests/utils_test.py index 454a14e8..716cde55 100644 --- a/tests/utils_test.py +++ b/tests/utils_test.py @@ -6,7 +6,7 @@ from docker.client import Client from docker.errors import DockerException from docker.utils import ( parse_repository_tag, parse_host, convert_filters, kwargs_from_env, - create_host_config, Ulimit, LogConfig + create_host_config, Ulimit, LogConfig, parse_bytes ) from docker.utils.ports import build_port_bindings, split_port from docker.auth import resolve_authconfig @@ -37,6 +37,12 @@ class UtilsTest(base.BaseTestCase): self.assertEqual(parse_repository_tag("url:5000/repo:tag"), ("url:5000/repo", "tag")) + def test_parse_bytes(self): + self.assertEqual(parse_bytes("512MB"), (536870912)) + self.assertEqual(parse_bytes("512M"), (536870912)) + self.assertRaises(DockerException, parse_bytes, "512MK") + self.assertRaises(DockerException, parse_bytes, "512L") + def test_parse_host(self): invalid_hosts = [ '0.0.0.0', From 396af7adbe480787ac9cb2ba866d6d2125b8dd30 Mon Sep 17 00:00:00 2001 From: Darren Shepherd Date: Mon, 25 May 2015 20:23:29 -0700 Subject: [PATCH 2/3] Typo in ReadonlyRootfs read_only parameter does not work because the wrong name is sent to Docker --- docker/utils/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker/utils/utils.py b/docker/utils/utils.py index 4110017a..e4a3c9e6 100644 --- a/docker/utils/utils.py +++ b/docker/utils/utils.py @@ -383,7 +383,7 @@ def create_host_config( host_config['PublishAllPorts'] = publish_all_ports if read_only is not None: - host_config['ReadOnlyRootFs'] = read_only + host_config['ReadonlyRootfs'] = read_only if dns_search: host_config['DnsSearch'] = dns_search From 1446b8c5eef57bb5f7392f73fada9ba4dc305de9 Mon Sep 17 00:00:00 2001 From: Aanand Prasad Date: Fri, 12 Jun 2015 12:36:59 -0400 Subject: [PATCH 3/3] Allow binds to be specified as a list of strings Signed-off-by: Aanand Prasad --- docker/utils/utils.py | 3 +++ docs/volumes.md | 13 +++++++++++++ tests/test.py | 30 ++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+) diff --git a/docker/utils/utils.py b/docker/utils/utils.py index e4a3c9e6..724af465 100644 --- a/docker/utils/utils.py +++ b/docker/utils/utils.py @@ -174,6 +174,9 @@ def convert_port_bindings(port_bindings): def convert_volume_binds(binds): + if isinstance(binds, list): + return binds + result = [] for k, v in binds.items(): if isinstance(v, dict): diff --git a/docs/volumes.md b/docs/volumes.md index de282140..db421557 100644 --- a/docs/volumes.md +++ b/docs/volumes.md @@ -19,3 +19,16 @@ container_id = c.create_container( }) ) ``` + +You can alternatively specify binds as a list. This code is equivalent to the +example above: + +```python +container_id = c.create_container( + 'busybox', 'ls', volumes=['/mnt/vol1', '/mnt/vol2'], + host_config=docker.utils.create_host_config(binds=[ + '/home/user1/:/mnt/vol2', + '/var/www:/mnt/vol1:ro', + ]) +) +``` diff --git a/tests/test.py b/tests/test.py index e0a9e345..97af11ee 100644 --- a/tests/test.py +++ b/tests/test.py @@ -808,6 +808,36 @@ class DockerClientTest(Cleanup, base.BaseTestCase): DEFAULT_TIMEOUT_SECONDS ) + def test_create_container_with_binds_list(self): + try: + self.client.create_container( + 'busybox', 'true', host_config=create_host_config( + binds=[ + "/tmp:/mnt/1:ro", + "/tmp:/mnt/2", + ], + ) + ) + except Exception as e: + self.fail('Command should not raise exception: {0}'.format(e)) + + args = fake_request.call_args + self.assertEqual(args[0][0], url_prefix + + 'containers/create') + expected_payload = self.base_create_payload() + expected_payload['HostConfig'] = create_host_config() + expected_payload['HostConfig']['Binds'] = [ + "/tmp:/mnt/1:ro", + "/tmp:/mnt/2", + ] + self.assertEqual(json.loads(args[1]['data']), expected_payload) + self.assertEqual(args[1]['headers'], + {'Content-Type': 'application/json'}) + self.assertEqual( + args[1]['timeout'], + DEFAULT_TIMEOUT_SECONDS + ) + def test_create_container_with_port_binds(self): self.maxDiff = None try: