Skip to content

Commit 62f1f36

Browse files
committed
Add subpath support to volumes in --mount option
To allow subpath mount as describes in [Compose Specs](https://github.com/compose-spec/compose-spec/blob/main/05-services.md), `refer_volume_over_mount` is changed from _True_ to **False** Compose entries exemple: - type: volume source: webservices target: /srv/www/vhosts/server1 read_only: true volume: subpath: server1 - type: volume source: webservices target: /srv/www/vhosts/server2 read_only: true volume: subpath: server2 - type: volume source: webservices target: /srv/www/vhosts/server2/uploads read_only: false volume: subpath: server2/uploads Runs podman with options --mount type=volume,source=webservices,target=/srv/www/vhosts/server1,ro --mount type=volume,source=webservices,target=/srv/www/vhosts/server2,ro,subpath=server2 --mount type=volume,source=webservices,target=/srv/www/vhosts/server2/uploads,subpath=server2/uploads Signed-off-by: fccagou <[email protected]>
1 parent a1f3bef commit 62f1f36

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

podman_compose.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,7 @@ def mount_desc_to_mount_args(mount_desc: dict[str, Any]) -> str:
459459
vol = mount_desc.get("_vol") if mount_type == "volume" else None
460460
source = vol["name"] if vol else mount_desc.get("source")
461461
target = mount_desc["target"]
462+
volume = mount_desc.get("volume") if mount_type == "volume" else None
462463
opts = []
463464
if mount_desc.get(mount_type, None):
464465
# TODO: we might need to add mount_dict[mount_type]["propagation"] = "z"
@@ -480,6 +481,11 @@ def mount_desc_to_mount_args(mount_desc: dict[str, Any]) -> str:
480481
selinux = bind_opts.get("selinux")
481482
if selinux is not None:
482483
opts.append(selinux)
484+
else:
485+
if volume is not None:
486+
subpath = volume.get("subpath")
487+
if subpath is not None:
488+
opts.append(f"subpath={subpath}")
483489
opts_str = ",".join(opts)
484490
if mount_type == "bind":
485491
return f"type=bind,source={source},destination={target},{opts_str}".rstrip(",")
@@ -1186,7 +1192,6 @@ async def container_to_args(
11861192
podman_args.extend(["--tmpfs", i])
11871193
for volume in cnt.get("volumes", []):
11881194
podman_args.extend(await get_mount_args(compose, cnt, volume))
1189-
11901195
await assert_cnt_nets(compose, cnt)
11911196
podman_args.extend(get_net_args(compose, cnt))
11921197

@@ -2064,7 +2069,7 @@ def __init__(self) -> None:
20642069
self.container_by_name: dict[str, Any]
20652070
self.services: dict[str, Any]
20662071
self.all_services: set[Any] = set()
2067-
self.prefer_volume_over_mount = True
2072+
self.prefer_volume_over_mount = False
20682073
self.x_podman: dict[PodmanCompose.XPodmanSettingKey, Any] = {}
20692074
self.merged_yaml: Any
20702075
self.yaml_hash = ""

tests/integration/selinux/test_podman_compose_selinux.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,15 @@ def test_selinux(self) -> None:
3838
inspect_out = json.loads(out)
3939
create_command_list = inspect_out[0].get("Config", []).get("CreateCommand", [])
4040
host_path = os.path.join(test_path(), "selinux", "host_test_text.txt")
41-
self.assertIn(f'{host_path}:/test_text.txt:z', create_command_list)
41+
try:
42+
# podman-compose.py: prefer_volume_over_mount set to False
43+
self.assertIn(
44+
f'type=bind,source={host_path},destination=/test_text.txt,z',
45+
create_command_list,
46+
)
47+
except AssertionError:
48+
# podman-compose.py: prefer_volume_over_mount set to True
49+
self.assertIn(f'{host_path}:/test_text.txt:z', create_command_list)
4250

4351
out, _ = self.run_subprocess_assert_returncode([
4452
"podman",
@@ -48,7 +56,14 @@ def test_selinux(self) -> None:
4856
inspect_out = json.loads(out)
4957
create_command_list = inspect_out[0].get("Config", []).get("CreateCommand", [])
5058
host_path = os.path.join(test_path(), "selinux", "host_test_text.txt")
51-
self.assertIn(f'{host_path}:/test_text.txt', create_command_list)
59+
try:
60+
# podman-compose.py: prefer_volume_over_mount set to False
61+
self.assertIn(
62+
f'type=bind,source={host_path},destination=/test_text.txt', create_command_list
63+
)
64+
except AssertionError:
65+
# podman-compose.py: prefer_volume_over_mount set to True
66+
self.assertIn(f'{host_path}:/test_text.txt', create_command_list)
5267
finally:
5368
out, _ = self.run_subprocess_assert_returncode([
5469
podman_compose_path(),

0 commit comments

Comments
 (0)