Skip to content

Commit 7532533

Browse files
bfirshshin-
authored andcommitted
Fix passing volumes to run with no host path
Technically we shouldn't be passing them as binds, but the daemon doesn't seem to mind. Fixes #1380 Signed-off-by: Ben Firshman <[email protected]>
1 parent 06838ff commit 7532533

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

docker/models/containers.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -885,5 +885,15 @@ def _create_container_args(kwargs):
885885
for p in sorted(port_bindings.keys())]
886886
binds = create_kwargs['host_config'].get('Binds')
887887
if binds:
888-
create_kwargs['volumes'] = [v.split(':')[1] for v in binds]
888+
create_kwargs['volumes'] = [_host_volume_from_bind(v) for v in binds]
889889
return create_kwargs
890+
891+
892+
def _host_volume_from_bind(bind):
893+
bits = bind.split(':')
894+
if len(bits) == 1:
895+
return bits[0]
896+
elif len(bits) == 2 and bits[1] in ('ro', 'rw'):
897+
return bits[0]
898+
else:
899+
return bits[1]

tests/unit/models_containers_test.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ def test_create_container_args(self):
101101
'/home/user1/:/mnt/vol2',
102102
'/var/www:/mnt/vol1:ro',
103103
'volumename:/mnt/vol3',
104+
'/volumewithnohostpath',
105+
'/anothervolumewithnohostpath:ro',
104106
],
105107
volumes_from=['container'],
106108
working_dir='/code'
@@ -118,6 +120,8 @@ def test_create_container_args(self):
118120
'/home/user1/:/mnt/vol2',
119121
'/var/www:/mnt/vol1:ro',
120122
'volumename:/mnt/vol3',
123+
'/volumewithnohostpath',
124+
'/anothervolumewithnohostpath:ro'
121125
],
122126
'BlkioDeviceReadBps': [{'Path': 'foo', 'Rate': 3}],
123127
'BlkioDeviceReadIOps': [{'Path': 'foo', 'Rate': 3}],
@@ -183,7 +187,13 @@ def test_create_container_args(self):
183187
tty=True,
184188
user='bob',
185189
volume_driver='some_driver',
186-
volumes=['/mnt/vol2', '/mnt/vol1', '/mnt/vol3'],
190+
volumes=[
191+
'/mnt/vol2',
192+
'/mnt/vol1',
193+
'/mnt/vol3',
194+
'/volumewithnohostpath',
195+
'/anothervolumewithnohostpath'
196+
],
187197
working_dir='/code'
188198
)
189199

0 commit comments

Comments
 (0)