Add moar docker awesomeness thanks to @thbkrkr/dotfiles

This commit is contained in:
Pierre Martin
2015-10-14 18:17:03 +02:00
parent 7e636bfe5b
commit cd8f0e7526
3 changed files with 140 additions and 0 deletions

75
bin/docker-cleanup-volumes.sh Executable file
View File

@@ -0,0 +1,75 @@
#!/bin/bash
#usage: sudo ./docker-cleanup-volumes.sh [--dry-run]
dockerdir=/var/lib/docker
volumesdir=${dockerdir}/volumes
vfsdir=${dockerdir}/vfs/dir
allvolumes=()
dryrun=false
function delete_volumes() {
targetdir=$1
echo
if [[ ! -d ${targetdir} ]]; then
echo "Directory ${targetdir} does not exist, skipping."
return
fi
echo "Delete unused volume directories from $targetdir"
for dir in $(ls -d ${targetdir}/* 2>/dev/null)
do
dir=$(basename $dir)
if [[ "${dir}" =~ [0-9a-f]{64} ]]; then
if [[ ${allvolumes[@]} =~ "${dir}" ]]; then
echo In use ${dir}
else
if [ "${dryrun}" = false ]; then
echo Deleting ${dir}
rm -rf ${targetdir}/${dir}
else
echo Would have deleted ${dir}
fi
fi
else
echo Not a volume ${dir}
fi
done
}
if [ $UID != 0 ]; then
echo "You need to be root to use this script."
exit 1
fi
docker_bin=$(which docker.io || which docker)
if [ -z "$docker_bin" ] ; then
echo "Please install docker. You can install docker by running \"wget -qO- https://get.docker.io/ | sh\"."
exit 1
fi
if [ "$1" = "--dry-run" ]; then
dryrun=true
else if [ -n "$1" ]; then
echo "Cleanup docker volumes: remove unused volumes."
echo "Usage: ${0##*/} [--dry-run]"
echo " --dry-run: dry run: display what would get removed."
exit 1
fi
fi
#All volumes from all containers
for container in `${docker_bin} ps -a -q --no-trunc`; do
#add container id to list of volumes, don't think these
#ever exists in the volumesdir but just to be safe
allvolumes+=${container}
#add all volumes from this container to the list of volumes
for vid in `${docker_bin} inspect --format='{{range $vol, $path := .Volumes}}{{$path}}{{"\n"}}{{end}}' ${container}`; do
if [[ "${vid##*/}" =~ [0-9a-f]{64} ]]; then
allvolumes+=("${vid##*/}")
fi
done
done
delete_volumes ${volumesdir}
delete_volumes ${vfsdir}

59
bin/docker-cleanup.sh Executable file
View File

@@ -0,0 +1,59 @@
#!/bin/bash -u
#
# Clean Docker containers and images.
#
# This script assumes that the docker images are tagged with a version (like a git sha1) and 'latest'.
#
# WARNING: only 'latest' Docker images for the given repo are kept after a cleanup!
#
# Example of how to tag a Docker image with a version and 'latest':
# $ docker build --rm -t $REPO/$NAME:$VERSION .
# $ docker tag -f $REPO/$NAME:$VERSION $(ORG)/$NAME:latest
#
# The repo to use to filter images to delete
REPO=$1
# Utils functions
countContainers() { docker ps -aq | wc -l; }
listStoppedContainers() { docker ps -a | grep Exit | awk '{print $1}'; }
countmages() { docker images | egrep -c "($REPO|none)"; }
listNotLatestImages() { docker images | grep $REPO | grep -v latest | awk '{printf "%s:%s\n", $1, $2}'; }
listUntaggedImages() { docker images -q -f dangling=true; }
log() { echo "[$(date +"%Y-%m-%dT%H:%M:%SZ")][docker-cleanup] $1"; }
totalContainers=$(countContainers)
totalImages=$(countmages)
log "Total containers: $totalContainers"
log "Total images: $totalImages"
# Delete all stopped containers
containerIds=$(listStoppedContainers)
if [[ "$containerIds" != "" ]]; then
log "Remove stopped containers..."
docker rm $containerIds
fi
# Delete not latest images
oldImages=$(listNotLatestImages)
if [[ "$oldImages" != "" ]]; then
log "Remove not latest images..."
docker rmi -f $oldImages
fi
# Delete all untagged images
untaggedImages=$(listUntaggedImages)
if [[ "$untaggedImages" != "" ]]; then
log "Remove untagged images..."
docker rmi $untaggedImages
fi
newTotalContainers=$(countContainers)
newTotalImages=$(countmages)
log "---"
log "Containers deleted: $(expr $totalContainers - $newTotalContainers)"
log "Images deleted: $(expr $totalImages - $newTotalImages)"
log "---"
log "Total containers: $newTotalContainers"
log "Total images: $newTotalImages"