When one play with docker for a while, we end up having lot of images and containers which are not used. Its important to delete them to reclaim resources.
here is a script / snippet that can be added to .bashrc / .profile or just to the current shell
# Use `docker-cleanup --dry-run` to see what would be deleted.
function docker-cleanup {
EXITED=$(docker ps -q -f status=exited)
DANGLING=$(docker images -q -f "dangling=true")
if [ "$1" == "--dry-run" ]; then
echo "==> Would stop containers:"
echo $EXITED
echo "==> And images:"
echo $DANGLING
else
if [ -n "$EXITED" ]; then
docker rm $EXITED
else
echo "No containers to remove."
fi
if [ -n "$DANGLING" ]; then
docker rmi $DANGLING
else
echo "No images to remove."
fi
fi
}
The comments used are derived from:
docker rm $(docker ps -q -f status=exited)
docker rm $(docker ps -q -f status=exited)
few more commands:
This is asper docker docs:
docker rm `docker ps -aq`
The ultimate clean up
docker system prune