Skip to content

down --volumes leaves anonymous volumes declared by Dockerfile VOLUME instructions behind #1521

Description

@te-horie

Describe the issue

Note

Related to: #378 (comment)
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.

for volume_name in await compose.podman.volume_ls():

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"]
    
    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:

    $ 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:
main...te-horie:podman-compose:down-remove-anonymous-volumes

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:

$ 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:

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

I agree with this opinion.

The help text for compose down also says:

help="Remove named volumes declared in the `volumes` section of the Compose file and "
"anonymous volumes attached to containers.",

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions