Skip to content

Commit d6b8476

Browse files
authored
Merge pull request #1180 from knarfS/add_rmi_arg
Add rmi argument for down command
2 parents 2e46ff0 + d80c31f commit d6b8476

File tree

7 files changed

+626
-5
lines changed

7 files changed

+626
-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
@@ -2786,7 +2786,7 @@ async def compose_up(compose: PodmanCompose, args):
27862786
diff_hashes = [i for i in hashes if i and i != compose.yaml_hash]
27872787
if args.force_recreate or len(diff_hashes):
27882788
log.info("recreating: ...")
2789-
down_args = argparse.Namespace(**dict(args.__dict__, volumes=False))
2789+
down_args = argparse.Namespace(**dict(args.__dict__, volumes=False, rmi=None))
27902790
await compose.commands["down"](compose, down_args)
27912791
log.info("recreating: done\n\n")
27922792
# args.no_recreate disables check for changes (which is not implemented)
@@ -2826,7 +2826,7 @@ async def handle_sigint():
28262826
log.info("Caught SIGINT or Ctrl+C, shutting down...")
28272827
try:
28282828
log.info("Shutting down gracefully, please wait...")
2829-
down_args = argparse.Namespace(**dict(args.__dict__, volumes=False))
2829+
down_args = argparse.Namespace(**dict(args.__dict__, volumes=False, rmi=None))
28302830
await compose.commands["down"](compose, down_args)
28312831
except Exception as e:
28322832
log.error("Error during shutdown: %s", e)
@@ -2915,7 +2915,6 @@ async def compose_down(compose: PodmanCompose, args):
29152915
containers = list(reversed(compose.containers))
29162916

29172917
down_tasks = []
2918-
29192918
for cnt in containers:
29202919
if cnt["_service"] in excluded:
29212920
continue
@@ -2936,8 +2935,10 @@ async def compose_down(compose: PodmanCompose, args):
29362935
if cnt["_service"] in excluded:
29372936
continue
29382937
await compose.podman.run([], "rm", [cnt["name"]])
2938+
2939+
orphaned_images = set()
29392940
if args.remove_orphans:
2940-
names = (
2941+
orphaned_containers = (
29412942
(
29422943
await compose.podman.output(
29432944
[],
@@ -2947,13 +2948,15 @@ async def compose_down(compose: PodmanCompose, args):
29472948
f"label=io.podman.compose.project={compose.project_name}",
29482949
"-a",
29492950
"--format",
2950-
"{{ .Names }}",
2951+
"{{ .Image }} {{ .Names }}",
29512952
],
29522953
)
29532954
)
29542955
.decode("utf-8")
29552956
.splitlines()
29562957
)
2958+
orphaned_images = {item.split()[0] for item in orphaned_containers}
2959+
names = {item.split()[1] for item in orphaned_containers}
29572960
for name in names:
29582961
await compose.podman.run([], "stop", [*podman_args, name])
29592962
for name in names:
@@ -2969,6 +2972,17 @@ async def compose_down(compose: PodmanCompose, args):
29692972
if volume_name in vol_names_to_keep:
29702973
continue
29712974
await compose.podman.run([], "volume", ["rm", volume_name])
2975+
if args.rmi:
2976+
images_to_remove = set()
2977+
for cnt in containers:
2978+
if cnt["_service"] in excluded:
2979+
continue
2980+
if args.rmi == "local" and not is_local(cnt):
2981+
continue
2982+
images_to_remove.add(cnt["image"])
2983+
images_to_remove.update(orphaned_images)
2984+
log.debug("images to remove: %s", images_to_remove)
2985+
await compose.podman.run([], "rmi", ["--ignore", "--force"] + list(images_to_remove))
29722986

29732987
if excluded:
29742988
return
@@ -3472,6 +3486,15 @@ def compose_down_parse(parser):
34723486
action="store_true",
34733487
help="Remove containers for services not defined in the Compose file.",
34743488
)
3489+
parser.add_argument(
3490+
"--rmi",
3491+
type=str,
3492+
nargs="?",
3493+
const="all",
3494+
choices=["local", "all"],
3495+
help="Remove images used by services. `local` remove only images that don't have a "
3496+
"custom tag. (`local` or `all`)",
3497+
)
34753498

34763499

34773500
@cmd_parse(podman_compose, "run")

tests/integration/up_down/Dockerfile

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
FROM docker.io/library/debian:bookworm-slim
2+
RUN apt-get update \
3+
&& apt-get install -y \
4+
dumb-init \
5+
&& apt-get autoremove
6+
RUN mkdir -p /mnt/test/

tests/integration/up_down/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
version: "3"
2+
volumes:
3+
web1_vol:
4+
web2_vol:
5+
services:
6+
web1:
7+
image: podman-compose-up-down-test
8+
build: .
9+
hostname: web1
10+
command: ["dumb-init", "sleep", "infinity"]
11+
volumes:
12+
- web1_vol:/mnt/test/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
version: "3"
2+
volumes:
3+
web1_vol:
4+
web2_vol:
5+
services:
6+
web1:
7+
image: podman-compose-up-down-test
8+
build: .
9+
hostname: web1
10+
command: ["dumb-init", "sleep", "infinity"]
11+
volumes:
12+
- web1_vol:/mnt/test/
13+
web2:
14+
image: docker.io/library/debian:up-down-test
15+
hostname: web2
16+
command: ["sleep", "infinity"]
17+
volumes:
18+
- web2_vol:/mnt/test/
19+

0 commit comments

Comments
 (0)