Last active
August 29, 2015 14:25
-
-
Save iadknet/2ae6943fabb062a5a4b7 to your computer and use it in GitHub Desktop.
Script to clean up docker images and containers to free up space
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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