Skip to content

Commit cefa68d

Browse files
committed
Implement rmi argument for down command
Fixes #387 Signed-off-by: Frank Stettner <[email protected]>
1 parent c46ecb2 commit cefa68d

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Add the `--rmi` argument to the `down` command to remove images.

podman_compose.py

+28-5
Original file line numberDiff line numberDiff line change
@@ -2770,7 +2770,7 @@ async def compose_up(compose: PodmanCompose, args):
27702770
diff_hashes = [i for i in hashes if i and i != compose.yaml_hash]
27712771
if args.force_recreate or len(diff_hashes):
27722772
log.info("recreating: ...")
2773-
down_args = argparse.Namespace(**dict(args.__dict__, volumes=False))
2773+
down_args = argparse.Namespace(**dict(args.__dict__, volumes=False, rmi=None))
27742774
await compose.commands["down"](compose, down_args)
27752775
log.info("recreating: done\n\n")
27762776
# args.no_recreate disables check for changes (which is not implemented)
@@ -2810,7 +2810,7 @@ async def handle_sigint():
28102810
log.info("Caught SIGINT or Ctrl+C, shutting down...")
28112811
try:
28122812
log.info("Shutting down gracefully, please wait...")
2813-
down_args = argparse.Namespace(**dict(args.__dict__, volumes=False))
2813+
down_args = argparse.Namespace(**dict(args.__dict__, volumes=False, rmi=None))
28142814
await compose.commands["down"](compose, down_args)
28152815
except Exception as e:
28162816
log.error("Error during shutdown: %s", e)
@@ -2899,7 +2899,6 @@ async def compose_down(compose: PodmanCompose, args):
28992899
containers = list(reversed(compose.containers))
29002900

29012901
down_tasks = []
2902-
29032902
for cnt in containers:
29042903
if cnt["_service"] in excluded:
29052904
continue
@@ -2920,8 +2919,10 @@ async def compose_down(compose: PodmanCompose, args):
29202919
if cnt["_service"] in excluded:
29212920
continue
29222921
await compose.podman.run([], "rm", [cnt["name"]])
2922+
2923+
orphaned_images = set()
29232924
if args.remove_orphans:
2924-
names = (
2925+
orphaned_containers = (
29252926
(
29262927
await compose.podman.output(
29272928
[],
@@ -2931,13 +2932,15 @@ async def compose_down(compose: PodmanCompose, args):
29312932
f"label=io.podman.compose.project={compose.project_name}",
29322933
"-a",
29332934
"--format",
2934-
"{{ .Names }}",
2935+
"{{ .Image }} {{ .Names }}",
29352936
],
29362937
)
29372938
)
29382939
.decode("utf-8")
29392940
.splitlines()
29402941
)
2942+
orphaned_images = {item.split()[0] for item in orphaned_containers}
2943+
names = {item.split()[1] for item in orphaned_containers}
29412944
for name in names:
29422945
await compose.podman.run([], "stop", [*podman_args, name])
29432946
for name in names:
@@ -2953,6 +2956,17 @@ async def compose_down(compose: PodmanCompose, args):
29532956
if volume_name in vol_names_to_keep:
29542957
continue
29552958
await compose.podman.run([], "volume", ["rm", volume_name])
2959+
if args.rmi:
2960+
images_to_remove = set()
2961+
for cnt in containers:
2962+
if cnt["_service"] in excluded:
2963+
continue
2964+
if args.rmi == "local" and not is_local(cnt):
2965+
continue
2966+
images_to_remove.add(cnt["image"])
2967+
images_to_remove.update(orphaned_images)
2968+
log.debug("images to remove: %s", images_to_remove)
2969+
await compose.podman.run([], "rmi", ["--ignore", "--force"] + list(images_to_remove))
29562970

29572971
if excluded:
29582972
return
@@ -3455,6 +3469,15 @@ def compose_down_parse(parser):
34553469
action="store_true",
34563470
help="Remove containers for services not defined in the Compose file.",
34573471
)
3472+
parser.add_argument(
3473+
"--rmi",
3474+
type=str,
3475+
nargs="?",
3476+
const="all",
3477+
choices=["local", "all"],
3478+
help="Remove images used by services. `local` remove only images that don't have a "
3479+
"custom tag. (`local` or `all`)",
3480+
)
34583481

34593482

34603483
@cmd_parse(podman_compose, "run")

0 commit comments

Comments
 (0)