Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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
echo "No containers to remove"
# 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
echo "No images to remove"
# 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
You can’t perform that action at this time.