Skip to content

Commit 86530df

Browse files
committed
fix: correctly modify volumes when normalizing services in a subdir
This fixes relative paths when using `extends` to include another service from a different directory Signed-off-by: Dominik Süß <[email protected]>
1 parent 0df9bf4 commit 86530df

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

podman_compose.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ def is_list(list_object: Any) -> bool:
5353
and hasattr(list_object, "__iter__")
5454
)
5555

56+
def is_relative_ref(path: str) -> bool:
57+
return path.startswith("./") or path.startswith(".:") or path.startswith("../") or path.startswith("..:")
58+
5659

5760
# identity filter
5861
def filteri(a: list[str]) -> list[str]:
@@ -1810,6 +1813,19 @@ def normalize_service(service: dict[str, Any], sub_dir: str = "") -> dict[str, A
18101813
for k, v in deps.items():
18111814
v.setdefault('condition', 'service_started')
18121815
service["depends_on"] = deps
1816+
if "volumes" in service and sub_dir:
1817+
new_volumes = []
1818+
for v in service["volumes"]:
1819+
if isinstance(v, str):
1820+
if is_relative_ref(v):
1821+
v = os.path.join(sub_dir,v)
1822+
elif isinstance(v, dict):
1823+
source = v["source"]
1824+
if is_relative_ref(source):
1825+
v["source"] = (os.path.join(sub_dir,source))
1826+
1827+
new_volumes.append(v)
1828+
service["volumes"] = new_volumes
18131829
return service
18141830

18151831

tests/unit/test_normalize_service.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,30 @@ def test_simple(self, input: dict[str, Any], expected: dict[str, Any]) -> None:
4747
{"build": {"context": "./dir-1", "dockerfile": "dockerfile-1"}},
4848
{"build": {"context": "./sub_dir/dir-1", "dockerfile": "dockerfile-1"}},
4949
),
50+
(
51+
{"volumes": ["./nested/relative:/mnt","../dir-in-parent:/mnt","..:/mnt",".:/mnt"]},
52+
{"volumes": ["./sub_dir/./nested/relative:/mnt","./sub_dir/../dir-in-parent:/mnt","./sub_dir/..:/mnt","./sub_dir/.:/mnt"]},
53+
),
54+
(
55+
{
56+
"volumes": [
57+
{
58+
"type": "bind",
59+
"source": "./nested/relative",
60+
"target": "/mnt",
61+
}
62+
]
63+
},
64+
{
65+
"volumes": [
66+
{
67+
"type": "bind",
68+
"source": "./sub_dir/./nested/relative",
69+
"target": "/mnt",
70+
}
71+
]
72+
},
73+
),
5074
])
5175
def test_normalize_service_with_sub_dir(
5276
self, input: dict[str, Any], expected: dict[str, Any]

0 commit comments

Comments
 (0)