Skip to content

Commit 740aa00

Browse files
committed
Add subpath support to volumes with type volume in --mount option
To allow subpath mount as describes in [Compose Specs](https://github.com/compose-spec/compose-spec/blob/main/05-services.md), 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 742fbdd commit 740aa00

File tree

2 files changed

+53
-5
lines changed

2 files changed

+53
-5
lines changed

podman_compose.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -478,11 +478,14 @@ def mount_desc_to_mount_args(mount_desc: dict[str, Any]) -> str:
478478
selinux = bind_opts.get("selinux")
479479
if selinux is not None:
480480
opts.append(selinux)
481-
if mount_type == "image":
482-
image_opts = mount_desc.get("image", {})
483-
subpath = image_opts.get("subpath")
481+
482+
# According to compose specifications https://docs.docker.com/reference/compose-file/services/#volumes
483+
# subpath can be used in image and volume mount type
484+
if mount_type in ["volume", "image"] and mount_desc.get(mount_type):
485+
subpath = mount_desc.get(mount_type, {}).get("subpath")
484486
if subpath is not None:
485487
opts.append(f"subpath={subpath}")
488+
486489
opts_str = ",".join(opts)
487490
if mount_type == "bind":
488491
return f"type=bind,source={source},destination={target},{opts_str}".rstrip(",")
@@ -580,9 +583,13 @@ async def get_mount_args(
580583
volume = get_mnt_dict(compose, cnt, volume)
581584
srv_name = cnt["_service"]
582585
mount_type = volume["type"]
583-
ignore_mount_type = {"image", "glob"}
586+
# By default, mount using -v is actually preferred over --mount.
587+
# In some case, options can only be set using --mount.
588+
# --mount is forced for type set in mount_over_volume_needed var.
589+
#
590+
mount_over_volume_needed = {"image", "glob", "volume"}
584591
await assert_volume(compose, volume)
585-
if compose.prefer_volume_over_mount and mount_type not in ignore_mount_type:
592+
if compose.prefer_volume_over_mount and mount_type not in mount_over_volume_needed:
586593
if mount_type == "tmpfs":
587594
# TODO: --tmpfs /tmp:rw,size=787448k,mode=1777
588595
args = volume["target"]

tests/unit/test_container_to_args.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -756,6 +756,47 @@ async def test_volumes_image_mount(
756756
],
757757
)
758758

759+
@parameterized.expand([
760+
(
761+
"without_subpath",
762+
{},
763+
"type=volume,source=volname,destination=/mnt/example",
764+
),
765+
(
766+
"with_subpath",
767+
{"volume": {"subpath": "path/into/volume"}},
768+
"type=volume,source=volname,destination=/mnt/example,subpath=path/into/volume",
769+
),
770+
])
771+
async def test_volumes_mount(
772+
self, test_name: str, volume_opts: dict, expected_mount_arg: str
773+
) -> None:
774+
c = create_compose_mock()
775+
cnt = get_minimal_container()
776+
cnt["_service"] = cnt["service_name"]
777+
778+
cnt["volumes"] = [
779+
{
780+
"type": "volume",
781+
"source": "volname",
782+
"target": "/mnt/example",
783+
**volume_opts,
784+
},
785+
]
786+
787+
args = await container_to_args(c, cnt)
788+
self.assertEqual(
789+
args,
790+
[
791+
"--name=project_name_service_name1",
792+
"-d",
793+
"--mount",
794+
expected_mount_arg,
795+
"--network=bridge:alias=service_name",
796+
"busybox",
797+
],
798+
)
799+
759800
@parameterized.expand([
760801
(
761802
"create_host_path_set_to_true",

0 commit comments

Comments
 (0)