Skip to content

Instantly share code, notes, and snippets.

@jnonon
Last active February 12, 2016 19:42
Show Gist options
  • Save jnonon/d07def7e6fb32cede7c1 to your computer and use it in GitHub Desktop.
Save jnonon/d07def7e6fb32cede7c1 to your computer and use it in GitHub Desktop.
Docker disk maintenance
#!/bin/bash
# References: https://lebkowski.name/docker-volumes/
# https://github.com/docker/docker/issues/18601
set -e
export JQ=/tmp/jq
if [ ! -e $JQ ]
then
wget -O $JQ https://s3.amazonaws.com/grovo-ops-public/jq-linux64
chmod a+x $JQ
fi
# remove exited containers:
docker ps --filter status=dead --filter status=exited -aq | xargs -r docker rm -v
# remove unused images:
docker images --no-trunc | grep '<none>' | awk '{ print $3 }' | xargs -r docker rmi
# remove unused volumes:
find '/var/lib/docker/volumes/' -mindepth 1 -maxdepth 1 -type d | grep -vFf \
<(docker ps -aq | xargs docker inspect | $JQ -r '.[] | .Mounts | .[] | .Name | select(.)') \
| xargs -r rm -fr
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment