Skip to content

Instantly share code, notes, and snippets.

@flomotlik
Created June 2, 2015 16:50
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 flomotlik/70500fe79d36d509b1cb to your computer and use it in GitHub Desktop.
Save flomotlik/70500fe79d36d509b1cb to your computer and use it in GitHub Desktop.
Docker Cleanup
function docker_cleanup {
# Find all containers that have exited
containers=`docker ps -a -q | xargs`
if [[ $containers ]]; then
# Remove exited containers
docker stop $containers
docker rm $containers
else
echo "No containers to remove"
fi
# Find all images that are not tagged
images=`docker images -a -q -f dangling=true | xargs`
if [[ $images ]]; then
# Remove untagged images
docker rmi $images
else
echo "No images to remove"
fi
# Clean all orphaned data volumes
docker run -v /var/run/docker.sock:/var/run/docker.sock -v /var/lib/docker:/var/lib/docker --rm martin/docker-cleanup-volumes
# List containers and images that remain
docker ps -a
docker images -a
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment