diff --git a/podman_compose.py b/podman_compose.py index cb16623f..7181b025 100755 --- a/podman_compose.py +++ b/podman_compose.py @@ -905,6 +905,13 @@ def run(self, podman_args, cmd='', cmd_args=None, wait=True, sleep=1, obj=None): time.sleep(sleep) return p + def volume_inspect_all(self): + output = self.output(["volume", "inspect", "--all"]).decode('utf-8') + return json.loads(output) + + def volume_rm(self, name): + return self.run(["volume", "rm", name]) + def normalize_service(service): for key in ("env_file", "security_opt"): if key not in service: continue @@ -1493,6 +1500,12 @@ def compose_down(compose, args): return for pod in compose.pods: compose.podman.run([], "pod", ["rm", pod["name"]], sleep=0) + if args.volumes: + volumes = compose.podman.volume_inspect_all() + for volume in volumes: + project = volume.get("Labels", {}).get("io.podman.compose.project") + if project == compose.project_name: + compose.podman.volume_rm(volume["Name"]) @cmd_run(podman_compose, 'ps', 'show status of containers') def compose_ps(compose, args): @@ -1652,6 +1665,12 @@ def compose_up_parse(parser): parser.add_argument("--exit-code-from", metavar='SERVICE', type=str, default=None, help="Return the exit code of the selected service container. Implies --abort-on-container-exit.") +@cmd_parse(podman_compose, 'down') +def compose_down_parse(parser): + parser.add_argument("-v", "--volumes", action='store_true', default=False, + help="Remove named volumes declared in the `volumes` section of the Compose file and " + "anonymous volumes attached to containers.") + @cmd_parse(podman_compose, 'run') def compose_run_parse(parser): parser.add_argument("-d", "--detach", action='store_true',