Skip to content

Commit bea6322

Browse files
jannefleischermilas
authored andcommitted
volume: added support for bind propagation
https://docs.docker.com/storage/bind-mounts/#configure-bind-propagation Signed-off-by: Janne Jakob Fleischer <janne.fleischer@ils-forschung.de> Signed-off-by: Milas Bowman <milas.bowman@docker.com>
1 parent 8b9ad78 commit bea6322

File tree

3 files changed

+60
-3
lines changed

3 files changed

+60
-3
lines changed

docker/api/container.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,11 @@ def create_container(self, image, command=None, hostname=None, user=None,
319319
'/var/www': {
320320
'bind': '/mnt/vol1',
321321
'mode': 'ro',
322+
},
323+
'/autofs/user1': {
324+
'bind': '/mnt/vol3',
325+
'mode': 'rw',
326+
'propagation': 'shared'
322327
}
323328
})
324329
)
@@ -329,10 +334,11 @@ def create_container(self, image, command=None, hostname=None, user=None,
329334
.. code-block:: python
330335
331336
container_id = client.api.create_container(
332-
'busybox', 'ls', volumes=['/mnt/vol1', '/mnt/vol2'],
337+
'busybox', 'ls', volumes=['/mnt/vol1', '/mnt/vol2', '/mnt/vol3'],
333338
host_config=client.api.create_host_config(binds=[
334339
'/home/user1/:/mnt/vol2',
335340
'/var/www:/mnt/vol1:ro',
341+
'/autofs/user1:/mnt/vol3:rw,shared',
336342
])
337343
)
338344

docker/utils/utils.py

Lines changed: 16 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',
@@ -141,6 +140,22 @@ def convert_volume_binds(binds):
141140
else:
142141
mode = 'rw'
143142

143+
# NOTE: this is only relevant for Linux hosts
144+
# (doesn't apply in Docker Desktop)
145+
propagation_modes = [
146+
'rshared',
147+
'shared',
148+
'rslave',
149+
'slave',
150+
'rprivate',
151+
'private',
152+
]
153+
if 'propagation' in v and v['propagation'] in propagation_modes:
154+
if mode:
155+
mode = ','.join([mode, v['propagation']])
156+
else:
157+
mode = v['propagation']
158+
144159
result.append(
145160
f'{k}:{bind}:{mode}'
146161
)

tests/integration/api_container_test.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,24 @@ 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+
def test_create_with_binds_rw_rshared(self):
546+
self.run_with_volume_propagation(
547+
False,
548+
'rshared',
549+
TEST_IMG,
550+
['touch', os.path.join(self.mount_dest, self.filename)],
551+
)
552+
container = self.run_with_volume_propagation(
553+
True,
554+
'rshared',
555+
TEST_IMG,
556+
['ls', self.mount_dest],
557+
)
558+
logs = self.client.logs(container).decode('utf-8')
559+
assert self.filename in logs
560+
inspect_data = self.client.inspect_container(container)
561+
self.check_container_data(inspect_data, True, 'rshared')
562+
545563
@requires_api_version('1.30')
546564
def test_create_with_mounts(self):
547565
mount = docker.types.Mount(
@@ -597,7 +615,7 @@ def test_create_with_volume_mount(self):
597615
assert mount['Source'] == mount_data['Name']
598616
assert mount_data['RW'] is True
599617

600-
def check_container_data(self, inspect_data, rw):
618+
def check_container_data(self, inspect_data, rw, propagation='rprivate'):
601619
assert 'Mounts' in inspect_data
602620
filtered = list(filter(
603621
lambda x: x['Destination'] == self.mount_dest,
@@ -607,6 +625,7 @@ def check_container_data(self, inspect_data, rw):
607625
mount_data = filtered[0]
608626
assert mount_data['Source'] == self.mount_origin
609627
assert mount_data['RW'] == rw
628+
assert mount_data['Propagation'] == propagation
610629

611630
def run_with_volume(self, ro, *args, **kwargs):
612631
return self.run_container(
@@ -624,6 +643,23 @@ def run_with_volume(self, ro, *args, **kwargs):
624643
**kwargs
625644
)
626645

646+
def run_with_volume_propagation(self, ro, propagation, *args, **kwargs):
647+
return self.run_container(
648+
*args,
649+
volumes={self.mount_dest: {}},
650+
host_config=self.client.create_host_config(
651+
binds={
652+
self.mount_origin: {
653+
'bind': self.mount_dest,
654+
'ro': ro,
655+
'propagation': propagation
656+
},
657+
},
658+
network_mode='none'
659+
),
660+
**kwargs
661+
)
662+
627663

628664
class ArchiveTest(BaseAPIIntegrationTest):
629665
def test_get_file_archive_from_container(self):

0 commit comments

Comments
 (0)