67

I just inspected my /var/lib/docker/volumes folder and discovered that is bursting with folders named as Docker UUIDs each of which contain a config.json file with contents along the lines of

{"ID":"UUID","Path":"/path/to/mounted/volume","IsBindMount":true,"Writable":true}

where

/path/to/mounted/volume

is the path to the folder on the host that was mounted on to a docker container with the -v switch at some point. I have such folders dating back to the start of my experiments with Docker, i.e. about 3 weeks ago.

The containers in question were stopped and docker rm'ed a long time ago so I cannot see that those entries are not past their sell-by date. This begs the question - is the left over I am seeing a bug or does one need to manually discard such entries from /var/lib/docker/volumes?

DroidOS
  • 8,530
  • 16
  • 99
  • 171

3 Answers3

153

For Docker 1.9 and up there's a native way:

List all orphaned volumes with

$ docker volume ls -qf dangling=true

Eliminate all of them with

$ docker volume rm $(docker volume ls -qf dangling=true)

Matt
  • 68,711
  • 7
  • 155
  • 158
Roman Usherenko
  • 4,659
  • 4
  • 17
  • 14
  • 1
    My client prints each volume name on a new line, so this command only removes the first volume then treats each other volume as a command. I used `sed` to remove the newlines: `docker volume rm $(docker volume ls -qf dangling=true | sed ':a;N;$!ba;s/\n/ /g')` – emc Oct 18 '16 at 19:48
  • @emc, for some, a for-loop may be more readable than using sed: `for v in $(docker volume ls -qf dangling=true); do docker volume rm "$v"; done;` – Kevin Nov 27 '16 at 19:55
  • 1
    @Kevin Xargs is pretty readable too: `docker volume ls -qf 'dangling=true' | xargs docker volume rm` – emc Nov 29 '16 at 21:10
  • 3
    ... and here's the equivalents for containers and images: `docker ps -q --filter "status=exited" | xargs docker rm` and `docker images -qf 'dangling=true' | xargs docker rmi` – emc Nov 29 '16 at 21:11
  • since v1.25 there is a builtin command to prune unsued volumes: `docker volume prune` – Tim Wißmann Jun 26 '19 at 22:47
28

From the Docker user guide:

If you remove containers that mount volumes, including the initial dbdata container, or the subsequent containers db1 and db2, the volumes will not be deleted. To delete the volume from disk, you must explicitly call docker rm -v against the last container with a reference to the volume. This allows you to upgrade, or effectively migrate data volumes between containers. - source

This is intentional behavior to avoid accidental data loss. You can use a tool like docker-cleanup-volumes to clean out unused volumes.

Kevan Ahlquist
  • 5,375
  • 1
  • 17
  • 25
10

For Docker 1.13+ and the ce/ee 17+ release numbers, use the volume prune command

docker volume prune

Unlike the dangling=true query, this will not remove "remote" driver based volumes.

Matt
  • 68,711
  • 7
  • 155
  • 158