Skip to content

Instantly share code, notes, and snippets.

@lucaspar
Last active July 12, 2024 19:52
Show Gist options
  • Save lucaspar/57d91d95cff220a808c7f2da4e233641 to your computer and use it in GitHub Desktop.
Save lucaspar/57d91d95cff220a808c7f2da4e233641 to your computer and use it in GitHub Desktop.
Lists dangling docker volumes and their sizes
#!/usr/bin/env bash
set -euo pipefail
# loop through each volume and calculate its size
function list_vols_and_sizes() {
VOLUMES=$1
for volume_name in $VOLUMES; do
SIZE=$(sudo du -sh "$DOCKER_VOLUME_DIR/${volume_name}/_data" 2>/dev/null | awk '{print $1}')
echo "${volume_name}, $SIZE"
done
}
# List dangling Docker volumes and their sizes.
function main() {
# directory where Docker volumes are stored.
DOCKER_VOLUME_DIR="$(docker info --format '{{.DockerRootDir}}')/volumes"
# list all Docker volumes
VOLUMES=$(docker volume ls -qf dangling=true)
echo "Found $(echo "$VOLUMES" | wc -w) dangling volumes"
echo "I need sudo to list volume sizes"
sudo -v
echo "Listing dangling volumes and their sizes..."
printf "%-30s %s\n" "VOLUME NAME" "SIZE"
list_vols_and_sizes "$VOLUMES" | sort -t, -k2 -h | column -t -s,
}
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment