--dry-run
parameter first. If it's not compatible with your system or Docker version it will delete all your volumes.Shellscript to delete orphaned docker volumes in /var/lib/docker/volumes and /var/lib/docker/vfs/dir
Docker version 1.4.1 up to ...
To delete orphaned volumes in Docker 1.9 and up you can also use the built-in docker volume prune
commands instead of this docker-cleanup-volumes script. The built-in command also deletes any directory in /var/lib/docker/volumes that is not a volume so make sure you didn't put anything in there you want to save:
List:
$ docker volume ls -qf dangling=true
Cleanup:
$ docker volume rm $(docker volume ls -qf dangling=true)
Or, handling a no-op better but Linux specific:
$ docker volume ls -qf dangling=true | xargs -r docker volume rm
--dry-run
parameter first to make sure it works okay and doesn't delete any volumes that shouldn't be deleted. If you feel bold and run it without --dry-run
anyway, make sure you did 1.$ sudo ./docker-cleanup-volumes.sh [--dry-run] [--verbose]
--dry-run : Use the --dry-run option to have the script print the volumes that would have been deleted without actually deleting them.
--verbose : Have the script output more information.
Run the "latest" forward compatible Docker client version (works with host Docker 1.4.x up to 1.13.x)
$ docker run -v /var/run/docker.sock:/var/run/docker.sock -v /var/lib/docker:/var/lib/docker --rm martin/docker-cleanup-volumes --dry-run
If you symlinked /var/lib/docker to somewhere else make sure you tell the Docker container where it is by providing the real path or by using readlink in volume parameter.
$ docker run -v /var/run/docker.sock:/var/run/docker.sock -v $(readlink -f /var/lib/docker):/var/lib/docker --rm martin/docker-cleanup-volumes --dry-run
It is also possible to use the host docker binary by mounting the host docker bin directory. This way you make sure the Docker versions are the same between host and container. For example:
$ docker run -v $(which docker):/bin/docker -v /var/run/docker.sock:/var/run/docker.sock -v $(readlink -f /var/lib/docker):/var/lib/docker --rm martin/docker-cleanup-volumes --dry-run