Skip to content

Instantly share code, notes, and snippets.

@lucaspar
Last active December 10, 2024 15:09
Show Gist options
  • Select an option

  • Save lucaspar/57d91d95cff220a808c7f2da4e233641 to your computer and use it in GitHub Desktop.

Select an option

Save lucaspar/57d91d95cff220a808c7f2da4e233641 to your computer and use it in GitHub Desktop.

Revisions

  1. lucaspar revised this gist Dec 10, 2024. 1 changed file with 8 additions and 8 deletions.
    16 changes: 8 additions & 8 deletions dckr-volumes
    Original 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="-----------,| ----,| -------------"
    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}"
    LINE="${volume_name},${SIZE},${CREATION_DATE}"
    lines+=("${LINE}")
    done
    sudo -k # comment out to avoid asking for password again on subsequent runs
    # sudo -k # commented out to avoid asking for password again on subsequent runs

    # sorting and formatting
    mapfile -t lines < <(printf "%s\n" "${lines[@]}" | sort -t, -k2 -h)
    mapfile -t lines_sorted < <(printf "%s\n" "${lines[@]}" | sort -t, -k2 -h)
    if [[ "$no_header" == false ]]; then
    lines=("${header_l1}" "${header_l2}" "${lines[@]}")
    lines_sorted=("${header_l1}" "${header_l2}" "${lines_sorted[@]}")
    fi
    mapfile -t lines < <(printf "%s\n" "${lines[@]}" | column -t -s,)
    mapfile -t lines_fmt < <(printf "%s\n" "${lines_sorted[@]}" | column -t -s, -o " | ")

    # print formatted output
    for line in "${lines[@]}"; do
    for line in "${lines_fmt[@]}"; do
    echo "${line}"
    done
    }
  2. lucaspar revised this gist Dec 10, 2024. 1 changed file with 8 additions and 0 deletions.
    8 changes: 8 additions & 0 deletions dckr-volumes
    Original 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
  3. lucaspar revised this gist Dec 10, 2024. 1 changed file with 96 additions and 12 deletions.
    108 changes: 96 additions & 12 deletions dckr-volumes
    Original 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() {
    VOLUMES=$1

    # 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}')
    echo "${volume_name}, $SIZE"
    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 all Docker volumes
    VOLUMES=$(docker volume ls -qf dangling=true)
    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))

    echo "Found $(echo "$VOLUMES" | wc -w) dangling volumes"
    echo "I need sudo to list volume sizes"
    sudo -v
    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

    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,
    if [[ "$no_header" == false ]]; then
    list_vols_and_sizes "${VOLUMES}"
    else
    list_vols_and_sizes "${VOLUMES}" --no-header
    fi

    }

    main
    main "$@"
  4. lucaspar revised this gist Jul 12, 2024. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions dckr-volumes
    Original 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() {
  5. lucaspar revised this gist Jul 12, 2024. 1 changed file with 13 additions and 1 deletion.
    14 changes: 13 additions & 1 deletion dckr-volumes
    Original 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"
  6. lucaspar revised this gist Jul 12, 2024. 1 changed file with 1 addition and 13 deletions.
    14 changes: 1 addition & 13 deletions dckr-volumes
    Original file line number Diff line number Diff line change
    @@ -1,16 +1,4 @@
    #!/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"
  7. lucaspar created this gist Jul 12, 2024.
    31 changes: 31 additions & 0 deletions dckr-volumes
    Original 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