From 1b673e85220522c6038957dc0bf997d9377385e4 Mon Sep 17 00:00:00 2001 From: ds Date: Thu, 26 Jun 2025 23:54:20 +0300 Subject: [PATCH 1/2] check if container already created --- podman_compose.py | 45 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 39 insertions(+), 6 deletions(-) diff --git a/podman_compose.py b/podman_compose.py index 89395fb8..6258aa94 100755 --- a/podman_compose.py +++ b/podman_compose.py @@ -3107,24 +3107,57 @@ async def compose_up(compose: PodmanCompose, args: argparse.Namespace) -> int | .decode("utf-8") .splitlines() ) + created_containers = [] diff_hashes = [i for i in hashes if i and i != compose.yaml_hash] - if (args.force_recreate and len(hashes) > 0) or len(diff_hashes): + if args.force_recreate and (hashes or diff_hashes): log.info("recreating: ...") down_args = argparse.Namespace(**dict(args.__dict__, volumes=False, rmi=None)) await compose.commands["down"](compose, down_args) log.info("recreating: done\n\n") - # args.no_recreate disables check for changes (which is not implemented) + else: + # args.no_recreate disables check for changes (which is not implemented) + # so let's just check for running containers + created_containers = ( + ( + await compose.podman.output( + [], + "ps", + [ + "--filter", + f"label=io.podman.compose.project={compose.project_name}", + "--filter", + "status=created", + "--filter", + "status=running", + "-a", + "--format", + "{{ .Names }}", + ], + ) + ) + .decode("utf-8") + .splitlines() + ) + log.debug("** Existing containers: %s", created_containers) await create_pods(compose) exit_code = 0 + for cnt in compose.containers: if cnt["_service"] in excluded: log.debug("** skipping: %s", cnt["name"]) continue - podman_args = await container_to_args(compose, cnt, detached=False, no_deps=args.no_deps) - subproc_exit_code = await compose.podman.run([], "create", podman_args) - if subproc_exit_code is not None and subproc_exit_code != 0: - exit_code = subproc_exit_code + + if args.force_recreate or cnt["name"] not in created_containers: + podman_args = await container_to_args( + compose, cnt, detached=False, no_deps=args.no_deps + ) + subproc_exit_code = await compose.podman.run([], "create", podman_args) + + if subproc_exit_code is not None and subproc_exit_code != 0: + exit_code = subproc_exit_code + else: + subproc_exit_code = 0 if not args.no_start and args.detach and subproc_exit_code is not None: container_exit_code = await run_container( From 69d75937378aa662567bcda692b13d9bc8d8e559 Mon Sep 17 00:00:00 2001 From: ds Date: Fri, 27 Jun 2025 01:18:42 +0300 Subject: [PATCH 2/2] update tests --- .../integration/service_scale/test_podman_compose_scale.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/tests/integration/service_scale/test_podman_compose_scale.py b/tests/integration/service_scale/test_podman_compose_scale.py index 38b51c0a..0b3fedbe 100644 --- a/tests/integration/service_scale/test_podman_compose_scale.py +++ b/tests/integration/service_scale/test_podman_compose_scale.py @@ -110,11 +110,7 @@ def test_scaleup_cli(self) -> None: "--scale", "service1=4", ]) - # error code 125 is expected as podman-compose complains about already used name - # "podman-compose_service1_1" for the 1st container - # Nevertheless, following containers are still created to scale as expected - # (in test case till 3 containers) - self.assertEqual(return_code, 125) + self.assertEqual(return_code, 0) output, _, return_code = self.run_subprocess([ podman_compose_path(),