Skip to content

Commit a41f147

Browse files
added support for propagation
Signed-off-by: Janne Jakob Fleischer <janne.fleischer@ils-forschung.de>
1 parent 84414e3 commit a41f147

3 files changed

Lines changed: 48 additions & 3 deletions

File tree

docker/api/container.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,11 @@ def create_container(self, image, command=None, hostname=None, user=None,
317317
'/var/www': {
318318
'bind': '/mnt/vol1',
319319
'mode': 'ro',
320+
},
321+
'/autofs/user1': {
322+
'bind': '/mnt/vol3',
323+
'mode': 'rw',
324+
'propagation': 'shared'
320325
}
321326
})
322327
)
@@ -327,10 +332,11 @@ def create_container(self, image, command=None, hostname=None, user=None,
327332
.. code-block:: python
328333
329334
container_id = client.api.create_container(
330-
'busybox', 'ls', volumes=['/mnt/vol1', '/mnt/vol2'],
335+
'busybox', 'ls', volumes=['/mnt/vol1', '/mnt/vol2', '/mnt/vol3'],
331336
host_config=client.api.create_host_config(binds=[
332337
'/home/user1/:/mnt/vol2',
333338
'/var/www:/mnt/vol1:ro',
339+
'/autofs/user1:/mnt/vol3:rw,shared',
334340
])
335341
)
336342

docker/utils/utils.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
from urllib.parse import urlparse, urlunparse
1919

20-
2120
URLComponents = collections.namedtuple(
2221
'URLComponents',
2322
'scheme netloc url params query fragment',
@@ -116,6 +115,7 @@ def convert_port_bindings(port_bindings):
116115

117116

118117
def convert_volume_binds(binds):
118+
119119
if isinstance(binds, list):
120120
return binds
121121

@@ -141,6 +141,13 @@ def convert_volume_binds(binds):
141141
mode = v['mode']
142142
else:
143143
mode = 'rw'
144+
145+
#this is only relevant for linux-hosts; doesn't work in Docker Desktop
146+
if 'propagation' in v and v['propagation'] in ('rshared','shared','rslave','slave','rprivate','private'):
147+
if mode:
148+
mode = ','.join([mode, v['propagation']])
149+
else:
150+
mode = v['propagation']
144151

145152
result.append(
146153
f'{k}:{bind}:{mode}'

tests/integration/api_container_test.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,20 @@ def test_create_with_binds_ro(self):
542542
inspect_data = self.client.inspect_container(container)
543543
self.check_container_data(inspect_data, False)
544544

545+
546+
def test_create_with_binds_rw_rshared(self):
547+
container = self.run_with_volume_propagation(
548+
False,
549+
'rshared',
550+
TEST_IMG,
551+
['touch', os.path.join(self.mount_dest, self.filename)],
552+
553+
)
554+
logs = self.client.logs(container).decode('utf-8')
555+
assert self.filename in logs
556+
inspect_data = self.client.inspect_container(container)
557+
self.check_container_data(inspect_data, True, 'rshared')
558+
545559
@requires_api_version('1.30')
546560
def test_create_with_mounts(self):
547561
mount = docker.types.Mount(
@@ -597,7 +611,7 @@ def test_create_with_volume_mount(self):
597611
assert mount['Source'] == mount_data['Name']
598612
assert mount_data['RW'] is True
599613

600-
def check_container_data(self, inspect_data, rw):
614+
def check_container_data(self, inspect_data, rw, propagation='rprivate'):
601615
assert 'Mounts' in inspect_data
602616
filtered = list(filter(
603617
lambda x: x['Destination'] == self.mount_dest,
@@ -607,6 +621,7 @@ def check_container_data(self, inspect_data, rw):
607621
mount_data = filtered[0]
608622
assert mount_data['Source'] == self.mount_origin
609623
assert mount_data['RW'] == rw
624+
assert mount_data['Propagation'] == propagation
610625

611626
def run_with_volume(self, ro, *args, **kwargs):
612627
return self.run_container(
@@ -623,6 +638,23 @@ def run_with_volume(self, ro, *args, **kwargs):
623638
),
624639
**kwargs
625640
)
641+
642+
def run_with_volume_propagation(self, ro, propagation, *args, **kwargs):
643+
return self.run_container(
644+
*args,
645+
volumes={self.mount_dest: {}},
646+
host_config=self.client.create_host_config(
647+
binds={
648+
self.mount_origin: {
649+
'bind': self.mount_dest,
650+
'ro': ro,
651+
'propagation': propagation
652+
},
653+
},
654+
network_mode='none'
655+
),
656+
**kwargs
657+
)
626658

627659

628660
class ArchiveTest(BaseAPIIntegrationTest):

0 commit comments

Comments
 (0)