Skip to content

Commit 5fa7576

Browse files
Luke Marsdenshin-
authored andcommitted
Add volume_driver param to client.create_container
- Add appropriate test which also asserts that volume names can be passed through to drivers. - Add new param to docs. Signed-off-by: Luke Marsden <[email protected]>
1 parent e980231 commit 5fa7576

File tree

4 files changed

+37
-3
lines changed

4 files changed

+37
-3
lines changed

docker/client.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ def create_container(self, image, command=None, hostname=None, user=None,
465465
network_disabled=False, name=None, entrypoint=None,
466466
cpu_shares=None, working_dir=None, domainname=None,
467467
memswap_limit=0, cpuset=None, host_config=None,
468-
mac_address=None, labels=None):
468+
mac_address=None, labels=None, volume_driver=None):
469469

470470
if isinstance(volumes, six.string_types):
471471
volumes = [volumes, ]
@@ -479,7 +479,8 @@ def create_container(self, image, command=None, hostname=None, user=None,
479479
self._version, image, command, hostname, user, detach, stdin_open,
480480
tty, mem_limit, ports, environment, dns, volumes, volumes_from,
481481
network_disabled, entrypoint, cpu_shares, working_dir, domainname,
482-
memswap_limit, cpuset, host_config, mac_address, labels
482+
memswap_limit, cpuset, host_config, mac_address, labels,
483+
volume_driver
483484
)
484485
return self.create_container_from_config(config, name)
485486

docker/utils/utils.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ def create_container_config(
489489
dns=None, volumes=None, volumes_from=None, network_disabled=False,
490490
entrypoint=None, cpu_shares=None, working_dir=None, domainname=None,
491491
memswap_limit=0, cpuset=None, host_config=None, mac_address=None,
492-
labels=None
492+
labels=None, volume_driver=None
493493
):
494494
if isinstance(command, six.string_types):
495495
command = shlex.split(str(command))
@@ -589,4 +589,5 @@ def create_container_config(
589589
'HostConfig': host_config,
590590
'MacAddress': mac_address,
591591
'Labels': labels,
592+
'VolumeDriver': volume_driver,
592593
}

docs/api.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ from. Optionally a single string joining container id's with commas
219219
* host_config (dict): A [HostConfig](hostconfig.md) dictionary
220220
* mac_address (str): The Mac Address to assign the container
221221
* labels (dict or list): A dictionary of name-value labels (e.g. `{"label1": "value1", "label2": "value2"}`) or a list of names of labels to set with empty values (e.g. `["label1", "label2"]`)
222+
* volume_driver (str): The name of a volume driver/plugin.
222223

223224
**Returns** (dict): A dictionary with an image 'Id' key and a 'Warnings' key.
224225

tests/test.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1380,6 +1380,37 @@ def test_create_container_with_labels_list(self):
13801380
args[1]['timeout'], DEFAULT_TIMEOUT_SECONDS
13811381
)
13821382

1383+
def test_create_container_with_named_volume(self):
1384+
try:
1385+
mount_dest = '/mnt'
1386+
volume_name = 'name'
1387+
self.client.create_container(
1388+
'busybox', 'true',
1389+
host_config=create_host_config(
1390+
binds={volume_name: {
1391+
"bind": mount_dest,
1392+
"ro": False
1393+
}}),
1394+
volume_driver='foodriver',
1395+
)
1396+
except Exception as e:
1397+
self.fail('Command should not raise exception: {0}'.format(e))
1398+
1399+
args = fake_request.call_args
1400+
self.assertEqual(args[0][0], url_prefix +
1401+
'containers/create')
1402+
expected_payload = self.base_create_payload()
1403+
expected_payload['VolumeDriver'] = 'foodriver'
1404+
expected_payload['HostConfig'] = create_host_config()
1405+
expected_payload['HostConfig']['Binds'] = ["name:/mnt:rw"]
1406+
self.assertEqual(json.loads(args[1]['data']), expected_payload)
1407+
self.assertEqual(args[1]['headers'],
1408+
{'Content-Type': 'application/json'})
1409+
self.assertEqual(
1410+
args[1]['timeout'],
1411+
DEFAULT_TIMEOUT_SECONDS
1412+
)
1413+
13831414
def test_resize_container(self):
13841415
try:
13851416
self.client.resize(

0 commit comments

Comments
 (0)