Last active
December 10, 2024 15:09
-
-
Save lucaspar/57d91d95cff220a808c7f2da4e233641 to your computer and use it in GitHub Desktop.
Revisions
-
lucaspar revised this gist
Dec 10, 2024 . 1 changed file with 8 additions and 8 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -23,8 +23,8 @@ function list_vols_and_sizes() { done # variables header_l1="VOLUME NAME,SIZE,CREATION DATE" header_l2="-----------,----,-------------" VOLUMES=${1} declare -a lines @@ -33,20 +33,20 @@ function list_vols_and_sizes() { for volume_name in $VOLUMES; do SIZE=$(sudo du -sh "${DOCKER_VOLUME_DIR}/${volume_name}/_data" 2>/dev/null | awk '{print $1}') CREATION_DATE=$(docker volume inspect "${volume_name}" --format '{{.CreatedAt}}' | cut -d'T' -f1) LINE="${volume_name},${SIZE},${CREATION_DATE}" lines+=("${LINE}") done # sudo -k # commented out to avoid asking for password again on subsequent runs # sorting and formatting mapfile -t lines_sorted < <(printf "%s\n" "${lines[@]}" | sort -t, -k2 -h) if [[ "$no_header" == false ]]; then lines_sorted=("${header_l1}" "${header_l2}" "${lines_sorted[@]}") fi mapfile -t lines_fmt < <(printf "%s\n" "${lines_sorted[@]}" | column -t -s, -o " | ") # print formatted output for line in "${lines_fmt[@]}"; do echo "${line}" done } -
lucaspar revised this gist
Dec 10, 2024 . 1 changed file with 8 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,12 @@ #!/usr/bin/env bash # Usage: dckr-volumes [OPTIONS] # Lists dangling Docker volumes and their sizes. # Options: # -h, --help Shows this help message and exit # -a, --all Lists all Docker volumes and their sizes, not only dangling ones # --no-header Hides the header # set -euo pipefail # loop through each volume and calculate its size -
lucaspar revised this gist
Dec 10, 2024 . 1 changed file with 96 additions and 12 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -3,30 +3,114 @@ set -euo pipefail # loop through each volume and calculate its size function list_vols_and_sizes() { # params no_header=false for arg in "$@"; do case $arg in --no-header) no_header=true ;; esac done # variables header_l1="VOLUME NAME,| SIZE,| CREATION DATE" header_l2="-----------,| ----,| -------------" VOLUMES=${1} declare -a lines # gather info sudo -v -p "[sudo] I need root access to list volume sizes: " for volume_name in $VOLUMES; do SIZE=$(sudo du -sh "${DOCKER_VOLUME_DIR}/${volume_name}/_data" 2>/dev/null | awk '{print $1}') CREATION_DATE=$(docker volume inspect "${volume_name}" --format '{{.CreatedAt}}' | cut -d'T' -f1) LINE="${volume_name},| ${SIZE},| ${CREATION_DATE}" lines+=("${LINE}") done sudo -k # comment out to avoid asking for password again on subsequent runs # sorting and formatting mapfile -t lines < <(printf "%s\n" "${lines[@]}" | sort -t, -k2 -h) if [[ "$no_header" == false ]]; then lines=("${header_l1}" "${header_l2}" "${lines[@]}") fi mapfile -t lines < <(printf "%s\n" "${lines[@]}" | column -t -s,) # print formatted output for line in "${lines[@]}"; do echo "${line}" done } function show_help_and_exit() { echo -e "\n\tUsage: \033[34m$(basename "$0") [OPTIONS]\033[0m\n" echo -e "Lists dangling Docker volumes and their sizes." echo -e "\n\tOptions:" echo -e "\t -h, --help Shows this help message and exit" echo -e "\t -a, --all Lists all Docker volumes and their sizes, not only dangling ones" echo -e "\t --no-header Hides the header" echo -e "\n" exit 0 } # List dangling Docker volumes and their sizes. function main() { # directory where Docker volumes are stored. DOCKER_VOLUME_DIR="$(docker info --format '{{.DockerRootDir}}')/volumes" list_mode="dangling" no_header=false while getopts "ah-:" opt; do case $opt in a) list_mode="all" ;; h) show_help_and_exit ;; -) case "${OPTARG}" in all) list_mode="all" ;; no-header) no_header=true ;; help) show_help_and_exit ;; *) echo "Invalid long option: --${OPTARG}" >&2 exit 1 ;; esac ;; \?) echo "Invalid short option: -${OPTARG}" >&2 exit 1 ;; esac done shift $((OPTIND - 1)) if [[ "$list_mode" == "dangling" ]]; then echo "Listing dangling volumes..." >&2 VOLUMES=$(docker volume ls -qf dangling=true) echo "Found $(echo "${VOLUMES}" | wc -w) dangling volumes" else echo "Listing all volumes..." >&2 VOLUMES=$(docker volume ls -q) echo "Found $(echo "${VOLUMES}" | wc -w) volumes (all volumes)" >&2 fi if [[ "$no_header" == false ]]; then list_vols_and_sizes "${VOLUMES}" else list_vols_and_sizes "${VOLUMES}" --no-header fi } main "$@" -
lucaspar revised this gist
Jul 12, 2024 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,5 @@ #!/usr/bin/env bash set -euo pipefail # loop through each volume and calculate its size function list_vols_and_sizes() { -
lucaspar revised this gist
Jul 12, 2024 . 1 changed file with 13 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,16 @@ #!/usr/bin/env bash # 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" -
lucaspar revised this gist
Jul 12, 2024 . 1 changed file with 1 addition and 13 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,16 +1,4 @@ () { # directory where Docker volumes are stored. DOCKER_VOLUME_DIR="$(docker info --format '{{.DockerRootDir}}')/volumes" -
lucaspar created this gist
Jul 12, 2024 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,31 @@ #!/usr/bin/env bash # 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