Add --remove-orphans on down command

Signed-off-by: Sander Hoentjen <shoentjen@antagonist.nl>
This commit is contained in:
Sander Hoentjen 2023-02-24 12:16:36 +01:00 committed by Muayyad Alsadi
parent e84451f4ea
commit b4c0792995
2 changed files with 26 additions and 1 deletions

View File

@ -98,7 +98,7 @@ _completeExecArgs() {
# complete the arguments for `podman-compose down` and return 0 # complete the arguments for `podman-compose down` and return 0
_completeDownArgs() { _completeDownArgs() {
down_opts="${help_opts} -v --volumes -t --timeout" down_opts="${help_opts} -v --volumes -t --timeout --remove-orphans"
if [[ ${prev} == "-t" || ${prev} == "--timeout" ]]; then if [[ ${prev} == "-t" || ${prev} == "--timeout" ]]; then
return 0 return 0
elif [[ ${cur} == -* ]]; then elif [[ ${cur} == -* ]]; then

View File

@ -2141,6 +2141,26 @@ def compose_down(compose, args):
if cnt["_service"] in excluded: if cnt["_service"] in excluded:
continue continue
compose.podman.run([], "rm", [cnt["name"]], sleep=0) compose.podman.run([], "rm", [cnt["name"]], sleep=0)
if args.remove_orphans:
names = (
compose.podman.output(
[],
"ps",
[
"--filter",
f"label=io.podman.compose.project={compose.project_name}",
"-a",
"--format",
"{{ .Names }}",
],
)
.decode("utf-8")
.splitlines()
)
for name in names:
compose.podman.run([], "stop", [*podman_args, name], sleep=0)
for name in names:
compose.podman.run([], "rm", [name], sleep=0)
if args.volumes: if args.volumes:
vol_names_to_keep = set() vol_names_to_keep = set()
for cnt in containers: for cnt in containers:
@ -2564,6 +2584,11 @@ def compose_down_parse(parser):
help="Remove named volumes declared in the `volumes` section of the Compose file and " help="Remove named volumes declared in the `volumes` section of the Compose file and "
"anonymous volumes attached to containers.", "anonymous volumes attached to containers.",
) )
parser.add_argument(
"--remove-orphans",
action="store_true",
help="Remove containers for services not defined in the Compose file.",
)
@cmd_parse(podman_compose, "run") @cmd_parse(podman_compose, "run")