summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--docker/utils/utils.py15
-rw-r--r--docs/volumes.md4
-rw-r--r--tests/test.py47
3 files changed, 63 insertions, 3 deletions
diff --git a/docker/utils/utils.py b/docker/utils/utils.py
index 84acca3..77bd7f3 100644
--- a/docker/utils/utils.py
+++ b/docker/utils/utils.py
@@ -180,8 +180,21 @@ def convert_volume_binds(binds):
result = []
for k, v in binds.items():
if isinstance(v, dict):
+ if 'ro' in v and 'mode' in v:
+ raise ValueError(
+ 'Binding cannot contain both "ro" and "mode": {}'
+ .format(repr(v))
+ )
+
+ if 'ro' in v:
+ mode = 'ro' if v['ro'] else 'rw'
+ elif 'mode' in v:
+ mode = v['mode']
+ else:
+ mode = 'rw'
+
result.append('{0}:{1}:{2}'.format(
- k, v['bind'], 'ro' if v.get('ro', False) else 'rw'
+ k, v['bind'], mode
))
else:
result.append('{0}:{1}:rw'.format(k, v))
diff --git a/docs/volumes.md b/docs/volumes.md
index db42155..16c3228 100644
--- a/docs/volumes.md
+++ b/docs/volumes.md
@@ -10,11 +10,11 @@ container_id = c.create_container(
host_config=docker.utils.create_host_config(binds={
'/home/user1/': {
'bind': '/mnt/vol2',
- 'ro': False
+ 'mode': 'rw',
},
'/var/www': {
'bind': '/mnt/vol1',
- 'ro': True
+ 'mode': 'ro',
}
})
)
diff --git a/tests/test.py b/tests/test.py
index f5815f0..bac9592 100644
--- a/tests/test.py
+++ b/tests/test.py
@@ -808,6 +808,53 @@ class DockerClientTest(Cleanup, base.BaseTestCase):
DEFAULT_TIMEOUT_SECONDS
)
+ def test_create_container_with_binds_mode(self):
+ try:
+ mount_dest = '/mnt'
+ mount_origin = '/tmp'
+ self.client.create_container(
+ 'busybox', 'true', host_config=create_host_config(
+ binds={mount_origin: {
+ "bind": mount_dest,
+ "mode": "z",
+ }}
+ )
+ )
+ except Exception as e:
+ self.fail('Command should not raise exception: {0}'.format(e))
+
+ args = fake_request.call_args
+ self.assertEqual(args[0][0], url_prefix +
+ 'containers/create')
+ expected_payload = self.base_create_payload()
+ expected_payload['HostConfig'] = create_host_config()
+ expected_payload['HostConfig']['Binds'] = ["/tmp:/mnt:z"]
+ self.assertEqual(json.loads(args[1]['data']), expected_payload)
+ self.assertEqual(args[1]['headers'],
+ {'Content-Type': 'application/json'})
+ self.assertEqual(
+ args[1]['timeout'],
+ DEFAULT_TIMEOUT_SECONDS
+ )
+
+ def test_create_container_with_binds_mode_and_ro_error(self):
+ try:
+ mount_dest = '/mnt'
+ mount_origin = '/tmp'
+ self.client.create_container(
+ 'busybox', 'true', host_config=create_host_config(
+ binds={mount_origin: {
+ "bind": mount_dest,
+ "mode": "z",
+ "ro": True,
+ }}
+ )
+ )
+ except ValueError:
+ return
+
+ self.fail('Command should raise ValueError')
+
def test_create_container_with_binds_list(self):
try:
self.client.create_container(