Skip to content

Instantly share code, notes, and snippets.

@epcim
Forked from mlebkowski/cleanup-docker.sh
Last active September 21, 2017 08:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save epcim/3495149fcee03b85a63befe58ae9223a to your computer and use it in GitHub Desktop.
Save epcim/3495149fcee03b85a63befe58ae9223a to your computer and use it in GitHub Desktop.
Cleanup docker prune aufs
#!/bin/bash
# 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
#!/bin/bash
# remove exited containers:
docker ps --filter status=dead --filter status=exited -aq | xargs -r docker rm -v
# remove unused images:
local images=`docker images --no-trunc`;
local lines=$(echo "$images" | awk '{print $2}' | grep -n "<none>" | cut -d: -f1 | sed 's/$/p/g');
lines=`echo $lines`;
lines=${lines// /;};
local image_ids=$(echo "$images" | awk '{print $3}' | sed -n "$lines");
[[ -n "${image_ids[@]}" ]] && docker rmi ${image_ids[@]}
# 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
docker system prune
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment