Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion podman_compose.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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


Expand Down Expand Up @@ -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:
Expand Down
20 changes: 20 additions & 0 deletions tests/unit/test_container_to_build_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
],
)
31 changes: 31 additions & 0 deletions tests/unit/test_normalize_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down