containers / containers/podman-compose

`down --volumes` leaves anonymous volumes declared by Dockerfile `VOLUME` instructions behind

Open Beginner friendly
#1,521 0 comments 1 reaction 0 assignees View on GitHub
Dominant language
Python
Stars
6.2k
Forks
622
PR merge metrics
No merged PRs in 30d

Description

**Describe the issue**

> [!NOTE]
> Related to: https://github.com/containers/podman-compose/issues/378#issuecomment-1019305661
> Unlike the original issue, which concerns `down --volumes` *with selected services*, this issue concerns anonymous volumes created by Dockerfile `VOLUME` instructions *during a full `down --volumes`*.

When shutting down the containers for the services defined in `compose.yaml` with the `down --volumes` command, podman-compose leaves anonymous volumes behind.
Even if the `--volumes` option is given, the subsequent volume search cannot find anonymous volumes because they do not have the `io.podman.compose.project` label.
https://github.com/containers/podman-compose/blob/47118746d89974f2d3f1e1971c2b84f87b1fbd9e/podman_compose.py#L4426

In contrast, Docker Compose removes anonymous volumes when it is invoked with `down --volumes`.

**To Reproduce**

1. Prepare a sample `Dockerfile` and `compose.yaml` as follows:

```
FROM busybox:latest
VOLUME ["/volume-repro"]
CMD ["sleep", "infinity"]
```
```yaml
services:
busybox:
init: true
build:
context: .
image: volume-repro
container_name: volume-repro
```
2. Execute `podman compose down -v` to shut down the services, but the anonymous volume declared by `VOLUME` in the `Dockerfile` still remains:

```console
$ podman compose up -d --build
...
$ podman inspect volume-repro | jq -r '.[]|.Mounts|.[]|.Name'
2d7cea363c6b564d74d2e4221b3a64e3afc5109ed8cc5d6d742ca8d38cc8df49
$ podman compose --verbose down -v
...
INFO:podman_compose:podman rm volume-repro
...
DEBUG:podman_compose:keep set()
INFO:podman_compose:['podman', 'volume', 'ls', '--noheading', '--filter', 'label=io.podman.compose.project=podman-compose', '--format', '{{.Name}}']
...
$ podman volume ls
DRIVER VOLUME NAME
local 2d7cea363c6b564d74d2e4221b3a64e3afc5109ed8cc5d6d742ca8d38cc8df49
```

**IMO: Preferable behavior**

Anonymous volumes attached to the containers should be removed when the `--volumes` option is specified.

**Actual behavior**

The container is removed, but its anonymous volume remains, as shown above.

**Possible fix**

podman-compose should pass `--volumes` to `podman rm` when the `--volumes` option is specified, as follows:
https://github.com/containers/podman-compose/compare/containers:e3df104...te-horie:58e7507
```diff
diff --git a/podman_compose.py b/podman_compose.py
index 3a90cc0..cbc3180 100755
--- a/podman_compose.py
+++ b/podman_compose.py
@@ -4389,7 +4389,7 @@ async def compose_down(compose: PodmanCompose, args: argparse.Namespace) -> None
for cnt in containers:
if cnt["_service"] in excluded:
continue
- await compose.podman.run([], "rm", [cnt["name"]])
+ await compose.podman.run([], "rm", (["--volumes"] if args.volumes else []) + [cnt["name"]])

orphaned_images = set()
if args.remove_orphans:
```

With this change, the anonymous volume is removed as expected:
```console
$ podman compose up -d --build
...
$ podman inspect volume-repro | jq -r '.[]|.Mounts|.[]|.Name'
38e85348e37272c1e358b3b33a0de92dc8253b83ff57cd4ae31bba6cf85aba2d
$ podman compose --verbose down -v
...
INFO:podman_compose:podman rm --volumes volume-repro
...
DEBUG:podman_compose:keep set()
INFO:podman_compose:['podman', 'volume', 'ls', '--noheading', '--filter', 'label=io.podman.compose.project=podman-compose', '--format', '{{.Name}}']
$ podman volume exists 38e85348e37272c1e358b3b33a0de92dc8253b83ff57cd4ae31bba6cf85aba2d || echo 'NONE'
NONE
```

**Discussion**

As @zzgab noted in [the previous discussion](https://github.com/containers/podman-compose/issues/378#issuecomment-1019305661):

> In my opinion, the compatibility with the reference behavior is preferable.

I agree with this opinion.

The help text for `compose down` also says:
https://github.com/containers/podman-compose/blob/47118746d89974f2d3f1e1971c2b84f87b1fbd9e/podman_compose.py#L4993-L4994

If the help text describes the intended behavior, podman-compose should also remove anonymous volumes created from image `VOLUME` declarations.
Otherwise, it should clarify that `--volumes` does not remove this type of anonymous volume.

Please share any comments or opinions on this issue.

Thank you!

**Environment:**
- OS: WSL
- podman version: 5.8.4
- podman-compose version: 1.6.0 (commit hash: `0f6537e9cfa38f6035ac57c1716b6d55dbaf3ca4`)

Contributor guide

Open the contributing guide

Research direction

Start in podman_compose.py at compose_down around the container removal call near line 4389, and review the compose down help text around lines 4993-4994. Reproduce with the Dockerfile and compose.yaml shown using podman compose up and down -v; done means the anonymous volume declared by VOLUME no longer remains.

Written by the indexing model from the issue text.

Assessment

Tech stack
docker, python
Domain
cli, devops
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
72/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.