Skip to content

Commit ea44212

Browse files
authored
Merge pull request #1888 from docker/1884-create_volumes_win32
Correctly parse volumes with Windows paths
2 parents 82e57a4 + 209ae24 commit ea44212

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

docker/models/containers.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import copy
2+
import ntpath
23
from collections import namedtuple
34

45
from ..api import APIClient
@@ -995,20 +996,25 @@ def _create_container_args(kwargs):
995996
# sort to make consistent for tests
996997
create_kwargs['ports'] = [tuple(p.split('/', 1))
997998
for p in sorted(port_bindings.keys())]
998-
binds = create_kwargs['host_config'].get('Binds')
999-
if binds:
1000-
create_kwargs['volumes'] = [_host_volume_from_bind(v) for v in binds]
999+
if volumes:
1000+
if isinstance(volumes, dict):
1001+
create_kwargs['volumes'] = [
1002+
v.get('bind') for v in volumes.values()
1003+
]
1004+
else:
1005+
create_kwargs['volumes'] = [
1006+
_host_volume_from_bind(v) for v in volumes
1007+
]
10011008
return create_kwargs
10021009

10031010

10041011
def _host_volume_from_bind(bind):
1005-
bits = bind.split(':')
1006-
if len(bits) == 1:
1007-
return bits[0]
1008-
elif len(bits) == 2 and bits[1] in ('ro', 'rw'):
1009-
return bits[0]
1012+
drive, rest = ntpath.splitdrive(bind)
1013+
bits = rest.split(':', 1)
1014+
if len(bits) == 1 or bits[1] in ('ro', 'rw'):
1015+
return drive + bits[0]
10101016
else:
1011-
return bits[1]
1017+
return bits[1].rstrip(':ro').rstrip(':rw')
10121018

10131019

10141020
ExecResult = namedtuple('ExecResult', 'exit_code,output')

tests/unit/models_containers_test.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ def test_create_container_args(self):
102102
'volumename:/mnt/vol3',
103103
'/volumewithnohostpath',
104104
'/anothervolumewithnohostpath:ro',
105+
'C:\\windows\\path:D:\\hello\\world:rw'
105106
],
106107
volumes_from=['container'],
107108
working_dir='/code'
@@ -120,7 +121,8 @@ def test_create_container_args(self):
120121
'/var/www:/mnt/vol1:ro',
121122
'volumename:/mnt/vol3',
122123
'/volumewithnohostpath',
123-
'/anothervolumewithnohostpath:ro'
124+
'/anothervolumewithnohostpath:ro',
125+
'C:\\windows\\path:D:\\hello\\world:rw'
124126
],
125127
'BlkioDeviceReadBps': [{'Path': 'foo', 'Rate': 3}],
126128
'BlkioDeviceReadIOps': [{'Path': 'foo', 'Rate': 3}],
@@ -191,7 +193,8 @@ def test_create_container_args(self):
191193
'/mnt/vol1',
192194
'/mnt/vol3',
193195
'/volumewithnohostpath',
194-
'/anothervolumewithnohostpath'
196+
'/anothervolumewithnohostpath',
197+
'D:\\hello\\world'
195198
],
196199
working_dir='/code'
197200
)

0 commit comments

Comments
 (0)