Skip to content

Instantly share code, notes, and snippets.

@iadknet
Last active August 29, 2015 14:25
Show Gist options
  • Save iadknet/2ae6943fabb062a5a4b7 to your computer and use it in GitHub Desktop.
Save iadknet/2ae6943fabb062a5a4b7 to your computer and use it in GitHub Desktop.
Script to clean up docker images and containers to free up space
#!/bin/bash
# Script to clean up unused containers and images
DANGLING_IMAGES=$(docker images -q --filter "dangling=true")
# Keeps the last 2 images/containers
IMAGE_IDS=$(docker images -q | tail -n +3)
CONTAINER_IDS=$(docker ps -a -q | tail -n +3)
if [ -n "$CONTAINER_IDS" ]; then
docker rm $CONTAINER_IDS
fi
if [ -n "$IMAGE_IDS" ]; then
docker rmi $IMAGE_IDS
fi
if [ -n "$DANGLING_IMAGES" ]; then
docker rmi $DANGLING_IMAGES
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment