Skip to content

Commit 6e3dba9

Browse files
committed
Merge branch 'aanand-allow-binds-list' into 1.3.0-rc0
2 parents cf178ad + 1446b8c commit 6e3dba9

File tree

4 files changed

+57
-2
lines changed

4 files changed

+57
-2
lines changed

docker/utils/utils.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,9 @@ def convert_port_bindings(port_bindings):
174174

175175

176176
def convert_volume_binds(binds):
177+
if isinstance(binds, list):
178+
return binds
179+
177180
result = []
178181
for k, v in binds.items():
179182
if isinstance(v, dict):
@@ -322,6 +325,9 @@ def parse_bytes(s):
322325
if len(s) == 0:
323326
s = 0
324327
else:
328+
if s[-2:-1].isalpha() and s[-1].isalpha():
329+
if (s[-1] == "b" or s[-1] == "B"):
330+
s = s[:-1]
325331
units = BYTE_UNITS
326332
suffix = s[-1].lower()
327333

@@ -380,7 +386,7 @@ def create_host_config(
380386
host_config['PublishAllPorts'] = publish_all_ports
381387

382388
if read_only is not None:
383-
host_config['ReadOnlyRootFs'] = read_only
389+
host_config['ReadonlyRootfs'] = read_only
384390

385391
if dns_search:
386392
host_config['DnsSearch'] = dns_search

docs/volumes.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,16 @@ container_id = c.create_container(
1919
})
2020
)
2121
```
22+
23+
You can alternatively specify binds as a list. This code is equivalent to the
24+
example above:
25+
26+
```python
27+
container_id = c.create_container(
28+
'busybox', 'ls', volumes=['/mnt/vol1', '/mnt/vol2'],
29+
host_config=docker.utils.create_host_config(binds=[
30+
'/home/user1/:/mnt/vol2',
31+
'/var/www:/mnt/vol1:ro',
32+
])
33+
)
34+
```

tests/test.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -808,6 +808,36 @@ def test_create_container_with_binds_rw(self):
808808
DEFAULT_TIMEOUT_SECONDS
809809
)
810810

811+
def test_create_container_with_binds_list(self):
812+
try:
813+
self.client.create_container(
814+
'busybox', 'true', host_config=create_host_config(
815+
binds=[
816+
"/tmp:/mnt/1:ro",
817+
"/tmp:/mnt/2",
818+
],
819+
)
820+
)
821+
except Exception as e:
822+
self.fail('Command should not raise exception: {0}'.format(e))
823+
824+
args = fake_request.call_args
825+
self.assertEqual(args[0][0], url_prefix +
826+
'containers/create')
827+
expected_payload = self.base_create_payload()
828+
expected_payload['HostConfig'] = create_host_config()
829+
expected_payload['HostConfig']['Binds'] = [
830+
"/tmp:/mnt/1:ro",
831+
"/tmp:/mnt/2",
832+
]
833+
self.assertEqual(json.loads(args[1]['data']), expected_payload)
834+
self.assertEqual(args[1]['headers'],
835+
{'Content-Type': 'application/json'})
836+
self.assertEqual(
837+
args[1]['timeout'],
838+
DEFAULT_TIMEOUT_SECONDS
839+
)
840+
811841
def test_create_container_with_port_binds(self):
812842
self.maxDiff = None
813843
try:

tests/utils_test.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from docker.errors import DockerException
77
from docker.utils import (
88
parse_repository_tag, parse_host, convert_filters, kwargs_from_env,
9-
create_host_config, Ulimit, LogConfig
9+
create_host_config, Ulimit, LogConfig, parse_bytes
1010
)
1111
from docker.utils.ports import build_port_bindings, split_port
1212
from docker.auth import resolve_authconfig
@@ -37,6 +37,12 @@ def test_parse_repository_tag(self):
3737
self.assertEqual(parse_repository_tag("url:5000/repo:tag"),
3838
("url:5000/repo", "tag"))
3939

40+
def test_parse_bytes(self):
41+
self.assertEqual(parse_bytes("512MB"), (536870912))
42+
self.assertEqual(parse_bytes("512M"), (536870912))
43+
self.assertRaises(DockerException, parse_bytes, "512MK")
44+
self.assertRaises(DockerException, parse_bytes, "512L")
45+
4046
def test_parse_host(self):
4147
invalid_hosts = [
4248
'0.0.0.0',

0 commit comments

Comments
 (0)