From f83993de0a7bbde043240beb85fd2eec65a21cf6 Mon Sep 17 00:00:00 2001 From: Ben Firshman Date: Tue, 7 Feb 2017 17:03:02 +0100 Subject: [PATCH] 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 --- docker/models/containers.py | 12 +++++++++++- tests/unit/models_containers_test.py | 12 +++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/docker/models/containers.py b/docker/models/containers.py index 02773922..330ac92c 100644 --- a/docker/models/containers.py +++ b/docker/models/containers.py @@ -885,5 +885,15 @@ def _create_container_args(kwargs): for p in sorted(port_bindings.keys())] binds = create_kwargs['host_config'].get('Binds') if binds: - create_kwargs['volumes'] = [v.split(':')[1] for v in binds] + create_kwargs['volumes'] = [_host_volume_from_bind(v) for v in binds] 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] + else: + return bits[1] diff --git a/tests/unit/models_containers_test.py b/tests/unit/models_containers_test.py index de727b0e..ae1bd12a 100644 --- a/tests/unit/models_containers_test.py +++ b/tests/unit/models_containers_test.py @@ -101,6 +101,8 @@ class ContainerCollectionTest(unittest.TestCase): '/home/user1/:/mnt/vol2', '/var/www:/mnt/vol1:ro', 'volumename:/mnt/vol3', + '/volumewithnohostpath', + '/anothervolumewithnohostpath:ro', ], volumes_from=['container'], working_dir='/code' @@ -118,6 +120,8 @@ class ContainerCollectionTest(unittest.TestCase): '/home/user1/:/mnt/vol2', '/var/www:/mnt/vol1:ro', 'volumename:/mnt/vol3', + '/volumewithnohostpath', + '/anothervolumewithnohostpath:ro' ], 'BlkioDeviceReadBps': [{'Path': 'foo', 'Rate': 3}], 'BlkioDeviceReadIOps': [{'Path': 'foo', 'Rate': 3}], @@ -183,7 +187,13 @@ class ContainerCollectionTest(unittest.TestCase): tty=True, user='bob', volume_driver='some_driver', - volumes=['/mnt/vol2', '/mnt/vol1', '/mnt/vol3'], + volumes=[ + '/mnt/vol2', + '/mnt/vol1', + '/mnt/vol3', + '/volumewithnohostpath', + '/anothervolumewithnohostpath' + ], working_dir='/code' )