Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions docker/models/containers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import copy
import ntpath
from collections import namedtuple

from ..api import APIClient
Expand Down Expand Up @@ -995,20 +996,25 @@ def _create_container_args(kwargs):
# sort to make consistent for tests
create_kwargs['ports'] = [tuple(p.split('/', 1))
for p in sorted(port_bindings.keys())]
binds = create_kwargs['host_config'].get('Binds')
if binds:
create_kwargs['volumes'] = [_host_volume_from_bind(v) for v in binds]
if volumes:
if isinstance(volumes, dict):
create_kwargs['volumes'] = [
v.get('bind') for v in volumes.values()
]
else:
create_kwargs['volumes'] = [
_host_volume_from_bind(v) for v in volumes
]
return create_kwargs


def _host_volume_from_bind(bind):
bits = bind.split(':')
if len(bits) == 1:
return bits[0]
elif len(bits) == 2 and bits[1] in ('ro', 'rw'):
return bits[0]
drive, rest = ntpath.splitdrive(bind)
bits = rest.split(':', 1)
if len(bits) == 1 or bits[1] in ('ro', 'rw'):
return drive + bits[0]
else:
return bits[1]
return bits[1].rstrip(':ro').rstrip(':rw')


ExecResult = namedtuple('ExecResult', 'exit_code,output')
Expand Down
7 changes: 5 additions & 2 deletions tests/unit/models_containers_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ def test_create_container_args(self):
'volumename:/mnt/vol3',
'/volumewithnohostpath',
'/anothervolumewithnohostpath:ro',
'C:\\windows\\path:D:\\hello\\world:rw'
],
volumes_from=['container'],
working_dir='/code'
Expand All @@ -120,7 +121,8 @@ def test_create_container_args(self):
'/var/www:/mnt/vol1:ro',
'volumename:/mnt/vol3',
'/volumewithnohostpath',
'/anothervolumewithnohostpath:ro'
'/anothervolumewithnohostpath:ro',
'C:\\windows\\path:D:\\hello\\world:rw'
],
'BlkioDeviceReadBps': [{'Path': 'foo', 'Rate': 3}],
'BlkioDeviceReadIOps': [{'Path': 'foo', 'Rate': 3}],
Expand Down Expand Up @@ -191,7 +193,8 @@ def test_create_container_args(self):
'/mnt/vol1',
'/mnt/vol3',
'/volumewithnohostpath',
'/anothervolumewithnohostpath'
'/anothervolumewithnohostpath',
'D:\\hello\\world'
],
working_dir='/code'
)
Expand Down