diff --git a/podman_compose.py b/podman_compose.py index 0d0957f6..55c84a87 100755 --- a/podman_compose.py +++ b/podman_compose.py @@ -54,6 +54,15 @@ def is_list(list_object: Any) -> bool: ) +def is_relative_ref(path: str) -> bool: + return ( + path.startswith("./") + or path.startswith(".:") + or path.startswith("../") + or path.startswith("..:") + ) + + # identity filter def filteri(a: list[str]) -> list[str]: return list(filter(lambda i: i, a)) @@ -1810,6 +1819,19 @@ def normalize_service(service: dict[str, Any], sub_dir: str = "") -> dict[str, A for k, v in deps.items(): v.setdefault('condition', 'service_started') service["depends_on"] = deps + if "volumes" in service and sub_dir: + new_volumes = [] + for v in service["volumes"]: + if isinstance(v, str): + if is_relative_ref(v): + v = os.path.join(sub_dir, v) + elif isinstance(v, dict): + source = v["source"] + if is_relative_ref(source): + v["source"] = os.path.join(sub_dir, source) + + new_volumes.append(v) + service["volumes"] = new_volumes return service @@ -2917,7 +2939,7 @@ def cleanup_temp_dockfile() -> None: if path_exists(dockerfile): # normalize dockerfile path, as the user could have provided unpredictable file formats - dockerfile = os.path.normpath(os.path.join(ctx, dockerfile)) + dockerfile = os.path.normpath(dockerfile) build_args.extend(["-f", dockerfile]) else: if custom_dockerfile_given: diff --git a/tests/unit/test_container_to_build_args.py b/tests/unit/test_container_to_build_args.py index bc553a01..d9629709 100644 --- a/tests/unit/test_container_to_build_args.py +++ b/tests/unit/test_container_to_build_args.py @@ -352,3 +352,23 @@ def test_build_ssh_array(self): '.', ], ) + + def test_containerfile_in_context(self): + c = create_compose_mock() + + cnt = get_minimal_container() + cnt['build']['context'] = "./subdir" + args = get_minimal_args() + args = container_to_build_args(c, cnt, args, lambda path: True) + self.assertEqual( + args, + [ + '-f', + 'subdir/Containerfile', + '-t', + 'new-image', + '--no-cache', + '--pull-always', + './subdir', + ], + ) diff --git a/tests/unit/test_normalize_service.py b/tests/unit/test_normalize_service.py index b58d6067..e7089238 100644 --- a/tests/unit/test_normalize_service.py +++ b/tests/unit/test_normalize_service.py @@ -47,6 +47,37 @@ def test_simple(self, input: dict[str, Any], expected: dict[str, Any]) -> None: {"build": {"context": "./dir-1", "dockerfile": "dockerfile-1"}}, {"build": {"context": "./sub_dir/dir-1", "dockerfile": "dockerfile-1"}}, ), + ( + {"volumes": ["./nested/relative:/mnt", "../dir-in-parent:/mnt", "..:/mnt", ".:/mnt"]}, + { + "volumes": [ + "./sub_dir/./nested/relative:/mnt", + "./sub_dir/../dir-in-parent:/mnt", + "./sub_dir/..:/mnt", + "./sub_dir/.:/mnt", + ] + }, + ), + ( + { + "volumes": [ + { + "type": "bind", + "source": "./nested/relative", + "target": "/mnt", + } + ] + }, + { + "volumes": [ + { + "type": "bind", + "source": "./sub_dir/./nested/relative", + "target": "/mnt", + } + ] + }, + ), ]) def test_normalize_service_with_sub_dir( self, input: dict[str, Any], expected: dict[str, Any]