How to use `docker volume prune`? (`docker volume prune` removing all volumes even when container is running)

On the docker documentation, it says the following:

docker volume prune === Remove all unused local volumes

To test that, I've set up a MongoDb container with the official latest image from docker hub. Doing so, created 2 volumes behind the scenes which are probably needed by the container to store the data.

When running docker volume ls now, I can see the two volumes with random names.

So let's say I would have multiple containers with volumes using random names. Now it would get difficult to know which of these are still in use and so I was expecting docker volume prune to help out here.

So I executed the command, expecting docker volume prune to delete nothing as my container is up and running with MongoDb. But what actually happened, is that all my volumes got removed. After that my container shutdown and could not be restarted.

I tried recreating this multiple times and every time even that my container is running, the command just deletes all volumes.

Anyone can explain that behavior?


Following command with the container id of my MongoDB image shows me the 2 volumes:

docker inspect -f '{{ .Mounts }}' *CONTAINER_ID*

So from my understanding docker knows that these volumes and the container belong together.

When I ask docker to show me the dangling volumes, it shows me the same volumes again:

docker volume ls --filter dangling=true

So when they are dangling it makes sense to me that the prune command removes them. But I clearly have the volumes in use with my container, so that's not clear to me.