Skip to content

Commit 1446b8c

Browse files
committed
Allow binds to be specified as a list of strings
Signed-off-by: Aanand Prasad <[email protected]>
1 parent be73aaf commit 1446b8c

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

docker/utils/utils.py

Lines changed: 3 additions & 0 deletions
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):

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:

0 commit comments

Comments
 (0)