chadoe / docker-cleanup-volumes Goto Github PK
View Code? Open in Web Editor NEWShellscript to delete orphaned docker volumes
Home Page: https://registry.hub.docker.com/u/martin/docker-cleanup-volumes/
License: MIT License
Shellscript to delete orphaned docker volumes
Home Page: https://registry.hub.docker.com/u/martin/docker-cleanup-volumes/
License: MIT License
Getting error when running against Docker 1.8.3, due to Cannot connect to the Docker daemon
when calling docker info
:
docker-cleanup-volumes nritholtz$ docker run -it -v /var/run/docker.sock:/var/run/docker.sock -v /var/lib/docker:/var/lib/docker --entrypoint=bash --rm martin/docker-cleanup-volumes
bash-4.3# docker info
FATA[0000] Cannot connect to the Docker daemon. Is 'docker -d' running on this host?
bash-4.3# ls /var/run/
docker.sock
bash-4.3# ls /var/lib/docker/
aufs graph linkgraph.db tmp vfs
containers init repositories-aufs trust volumes
bash-4.3# exit
exit
docker-cleanup-volumes nritholtz$ docker info
Containers: 0
Images: 280
Storage Driver: aufs
Root Dir: /mnt/sda1/var/lib/docker/aufs
Backing Filesystem: extfs
Dirs: 280
Dirperm1 Supported: true
Execution Driver: native-0.2
Logging Driver: json-file
Kernel Version: 4.1.10-boot2docker
Operating System: Boot2Docker 1.8.3 (TCL 6.4); master : af8b089 - Mon Oct 12 18:56:54 UTC 2015
CPUs: 4
Total Memory: 1.955 GiB
Name: default
ID: IXJO:JTHJ:ZABX:BB3M:ZC2L:6TQR:H7QO:MLW2:YKDX:5IQX:SYAW:KQPW
Debug mode (server): true
File Descriptors: 12
Goroutines: 18
System Time: 2015-10-13T18:58:42.28216136Z
EventsListeners: 0
Init SHA1:
Init Path: /usr/local/bin/docker
Docker Root Dir: /mnt/sda1/var/lib/docker
Username: nritholtz
Registry: https://index.docker.io/v1/
Labels:
provider=virtualbox
First off, thanks for this handy script!
I ran into an issue because my build server had ~85k orphaned volumes (image building server from late 2014 :) ) where the ls -d ${targetdir}/*
command was failing.
I was able to replace it with find ${targetdir} -maxdepth 1 -type d
and continue on my merry way, however I don't have the time or criteria to test and ensure that works in all cases, so I figured an issue was better than a PR.
Thanks again and hope this helps!
regarding:
https://github.com/chadoe/docker-cleanup-volumes/blob/master/docker-cleanup-volumes.sh#L62
Maybe this should just be an echo warning in case of access-denied errors.
Ran the docker and this just went through and deleted all volumes period even on running containers, now I get to rebuild them. Joy is me.
docker@boot2docker:$ sh docker-cleanup-volumes.sh$
docker-cleanup-volumes.sh: line 10: syntax error: unexpected "("
docker@boot2docker:
When you say, in the precautions:
Don't use with boot2docker, docker machine or any tools that use a virtual machine to run docker, unless you run the script on the virtual machine itself (meaning don't run this script or docker command from your MAC).
I'm using Docker 1.9 on OSX with docker-machine, is it safe for me to use the command provided docker volume rm $(docker volume ls -qf dangling=true)
from my host? I did it from within the VM as you suggested, but I'm wondering if the warning is just for running your script on versions < 1.9?
Template parsing error: template: :1:23: executing "" at <.Volumes>: map has no entry for key "Volumes"
Template parsing error: template: :1:23: executing "" at <.Volumes>: map has no entry for key "Volumes"
Template parsing error: template: :1:23: executing "" at <.Volumes>: map has no entry for key "Volumes"
Template parsing error: template: :1:23: executing "" at <.Volumes>: map has no entry for key "Volumes"
Delete unused volume directories from /var/lib/docker/volumes
In use 64827c5ad81d0d94274364621394df01967664b4ece2f909bb4b72cb535aecae
Directory /var/lib/docker/vfs/dir does not exist or is empty, skipping.
docker version
Client:
Version: 1.10.3
API version: 1.22
Go version: go1.5.3
Git commit: 20f81dd
Built: Thu Mar 10 15:54:52 2016
OS/Arch: linux/amd64
Server:
Version: 1.10.3
API version: 1.22
Go version: go1.5.3
Git commit: 20f81dd
Built: Thu Mar 10 15:54:52 2016
OS/Arch: linux/amd64
just one volume directory:
root@boot2docker:/mnt/sda1/var/lib/docker/volumes# ls -la
total 12
drwx------ 3 root root 4096 Jun 24 21:21 .
drwxr-xr-x 9 root root 4096 Jun 24 21:24 ..
drwxr-xr-x 3 root root 4096 Jun 24 21:21 bd1e586a629637ffb1153847260e63f810c43d7f9f28f382eecacf9f4a5eb749
and as you can see it IS used by a running container:
✗ docker inspect --format '{{.Volumes}}' database ruby-2.2.0 (master) deedf83
map[/var/lib/postgresql/data:/mnt/sda1/var/lib/docker/volumes/bd1e586a629637ffb1153847260e63f810c43d7f9f28f382eecacf9f4a5eb749/_data]
now running the script:
✗ 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 ruby-2.2.0 (master) deedf83
Delete unused volume directories from /var/lib/docker/volumes
Would have deleted bd1e586a629637ffb1153847260e63f810c43d7f9f28f382eecacf9f4a5eb749
Directory /var/lib/docker/vfs/dir does not exist, skipping.
If you configured an alternative dockerdir (default: /var/lib/docker) the script doesn't work. It would be nice to get this variable configurable.
From a code inspection it seems to me that this will delete bind-mount volumes from /var/lib/docker/volumes, even if they are in use.
For bind-mount volumes (that is, when you do docker run -v /hostpath:/containerpath
), docker inspect
prints something like this:
"Volumes": {
"/containerpath": "/hostpath"
},
...so that volume isn't added into allvolumes
in your script, but you do delete it from /var/lib/docker/volumes.
I was using https://github.com/spotify/docker-gc, but it deletes volumes associated this containers that were never run such as with Data Only Containers.
Will this script remove them as well or is it safe?
Hello!
I've noticed this script removing running container volumes on centos + docker 1.4.1 (in boot2docker)... using the docker container way to run the script.
What more information can I provide?
Topper
When running from docker - there is ability to use host docker binary if we set it via host volume. For example:
$ docker run -v $(which docker):/bin/docker -v /var/run/docker.sock:/var/run/docker.sock -v /var/lib/docker:/var/lib/docker --rm martin/docker-cleanup-volumes --dry-run
Looks different, but it allows to feel free which docker version is used on host and no need to keep builds for last n docker versions.
I haven't had any issues, but a potentially damaging script like this should default to --dry-run
to avoid issues like #19. git clean
defaults to following this practice:
$ git clean
fatal: clean.requireForce defaults to true and neither -i, -n nor -f given; refusing to clean
$ git clean -n
Would remove Dockerfile
Would remove docker-compose.yml
Hallo,
Learning docker, I installed docker toolbox on my Mac and follow the docker book from James Turnbull.
$ docker-machine --version
docker-machine version 0.4.0 (9d0dc7a)
Run from my Mac.
$ 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 --verbose
dockerdir -> /var/lib/docker
dockerdir_match -> /var/lib/docker
Processing volumepath /var/lib/docker for container cf4df59e1a292b37ee52bd68cbe052ba64b9a5ba3759df37fad66bbcf1c50a68
Processing volumepath /var/run/docker.sock for container cf4df59e1a292b37ee52bd68cbe052ba64b9a5ba3759df37fad66bbcf1c50a68
Processing volumepath /Users/serege/thuis/docker/james_blog for container d03b3329be410d6d03f3484eb8b5ee1f2df8749bc60b67b08b1bf0549d71d4df
Processing volumepath /mnt/sda1/var/lib/docker/volumes/967e423a2c907eb4aa0ccbc1e33ffca3f8051859c4198f8247d1a7903a738132/_data for container d03b3329be410d6d03f3484eb8b5ee1f2df8749bc60b67b08b1bf0549d71d4df
Delete unused volume directories from /var/lib/docker/volumes
Would have deleted 967e423a2c907eb4aa0ccbc1e33ffca3f8051859c4198f8247d1a7903a738132
Directory /var/lib/docker/vfs/dir does not exist, skipping.
Why would it delete the volume when is has a volume path? Or did I need to run an other command?
Regards,
Gerrit
On my system, /var/lib/docker
is symlinked to another directory on the system. After running docker-cleanup-volumes.sh
it removes volumes for my data-only containers that are not currently running (and possibly others and I just didn't notice it). Changing dockerdir
in the script to the actual location fixes this for me.
FYI I ran your dockerized script but it kept outputting the below "grep" warning about "recursive directory loop". Probably nothing for you to do as a fix, just posting in case other users see the error.
Deleting the volume mentioned in the message fixed the problem for me -- it was an old volume for jenkins.
Below are my steps. I first ran "docker-machine ssh dev" to access my docker server. It's a virtual box / boot2docker machine on my macbook, continuously upgraded from ~1.6 to 1.9.1.
root@dev:/mnt/sda1/var/lib/docker# docker version
Client:
Version: 1.9.1
API version: 1.21
Go version: go1.4.3
Git commit: a34a1d5
Built: Fri Nov 20 17:56:04 UTC 2015
OS/Arch: linux/amd64
Server:
Version: 1.9.1
API version: 1.21
Go version: go1.4.3
Git commit: a34a1d5
Built: Fri Nov 20 17:56:04 UTC 2015
OS/Arch: linux/amd64
root@dev:/mnt/sda1/var/lib/docker# 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
...
grep: warning: /var/lib/docker/volumes/0eb33134d5df1f8c88afd051fe4725c0bc53d8f6e3068ce3f64edd8ca4f4a144/_data/sys/class/power_supply/AC/device/subsystem/devices/LNXVIDEO:00/input/input6/subsystem/event6/device/device/subsystem/devices/0000:00:0d.0/ata1/ata_port/ata1/subsystem/ata5/device/host4/subsystem/devices/host25/scsi_host/host25/subsystem/host0/device/target0:0:0/0:0:0:0/scsi_device/0:0:0:0/subsystem/1:0:0:0/device/block/sda/sda1/subsystem/loop3/bdi/subsystem/7:2/subsystem: recursive directory loop
That does seem like a weird directory:
root@dev:/mnt/sda1/var/lib/docker# ls -l /var/lib/docker/volumes/0eb33134d5df1f8c88afd051fe4725c0bc53d8f6e3068ce3f64edd8ca4f4a144/_data/sys/class/power_supply/AC/device/subsyste
m/devices/
total 80
lrwxrwxrwx 1 root root 63 Sep 4 04:11 ACPI0003:00 -> ../../../devices/LNXSYSTM:00/LNXSYBUS:00/PNP0A03:00/ACPI0003:00
...
That particular volume appears to be for my Jenkins container (/var/lib/jenkins) from a few months ago. Deleting (rm -rf /var/lib/docker/volumes/0eb33134d5df1f8c88afd051fe4725c0bc53d8f6e3068ce3f64edd8ca4f4a144
) it allowed your script to run correctly.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.