Last active
July 23, 2025 09:24
-
-
Save jim60105/48e300ab09674ca13c9a944e971cfa92 to your computer and use it in GitHub Desktop.
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 characters
| #!/usr/bin/env zsh | |
| # Copyright (C) 2025 Jim Chen <[email protected]>, licensed under GPL-3.0-or-later | |
| # | |
| # Container Image Size Checker Script | |
| # Purpose: Quickly check the size of container images using skopeo and jq. | |
| # Features: | |
| # - Parses multiple repository:tag arguments | |
| # - Uses skopeo inspect and jq to extract image and layer sizes | |
| # - Formats and displays size in B, KB, MB, or GB | |
| # - Sums total size for all images | |
| # - Handles errors and invalid arguments gracefully | |
| # - Outputs results in a readable table format | |
| # Requirements: | |
| # - skopeo | |
| # - jq | |
| # Enable zsh options for better array support | |
| setopt LOCAL_OPTIONS | |
| setopt EXTENDED_GLOB | |
| # Check for required tools | |
| if ! command -v skopeo >/dev/null 2>&1; then | |
| print "ERROR: skopeo is required but not installed" >&2 | |
| exit 1 | |
| fi | |
| if ! command -v jq >/dev/null 2>&1; then | |
| print "ERROR: jq is required but not installed" >&2 | |
| exit 1 | |
| fi | |
| # Function to format file size | |
| format_size() { | |
| local size=$1 | |
| if (( size < 1024 )); then | |
| printf "%d B" $size | |
| elif (( size < 1048576 )); then | |
| printf "%.2f KB" $((size / 1024.0)) | |
| elif (( size < 1073741824 )); then | |
| printf "%.2f MB" $((size / 1048576.0)) | |
| else | |
| printf "%.2f GB" $((size / 1073741824.0)) | |
| fi | |
| } | |
| # Check arguments | |
| if [[ $# -eq 0 ]]; then | |
| print "Usage: $0 <repository:tag> [repository:tag] ..." | |
| print "Example: $0 ghcr.io/jim60105/yt-dlp:latest docker.io/library/ubuntu:22.04" | |
| exit 1 | |
| fi | |
| print "π Checking container image sizes" | |
| print "============================================================" | |
| typeset -i TOTAL_SIZE=0 | |
| typeset -i result_count=0 | |
| for ITEM in "$@"; do | |
| # Parse repository:tag using zsh parameter expansion | |
| if [[ ! "$ITEM" =~ ":" ]]; then | |
| print " β Invalid argument format: $ITEM (use repository:tag format)" | |
| continue | |
| fi | |
| local REPO="${ITEM%:*}" | |
| local TAG="${ITEM##*:}" | |
| if [[ -z "$REPO" || -z "$TAG" || "$REPO" == "$TAG" ]]; then | |
| print " β Invalid argument format: $ITEM (use repository:tag format)" | |
| continue | |
| fi | |
| local IMAGE_URL="docker://$REPO:$TAG" | |
| print "π Checking image: $REPO:$TAG" | |
| # Use skopeo inspect and parse size | |
| local SIZE_INFO="" | |
| SIZE_INFO=$(skopeo inspect "$IMAGE_URL" 2>/dev/null | jq -r ' | |
| [ | |
| (.config.size // 0), | |
| (.LayersData // .layers // [] | map(.Size // .size // 0) | add // 0) | |
| ] | add | |
| ' 2>/dev/null) | |
| if [[ "$SIZE_INFO" != "null" ]] && [[ "$SIZE_INFO" -gt 0 ]]; then | |
| local SIZE_STR=$(format_size $SIZE_INFO) | |
| printf " %-40s | %10s | %15s bytes\n" "$REPO:$TAG" "$SIZE_STR" "$(printf "%'d" "$SIZE_INFO")" | |
| (( TOTAL_SIZE += SIZE_INFO )) | |
| (( result_count++ )) | |
| else | |
| print " β Unable to get image size ($REPO:$TAG)" | |
| fi | |
| done | |
| if (( result_count > 0 )); then | |
| print "------------------------------------------------------------" | |
| local TOTAL_SIZE_STR=$(format_size $TOTAL_SIZE) | |
| printf "π¦ %-40s | %10s | %15s bytes\n" "Total:" "$TOTAL_SIZE_STR" "$(printf "%'d" "$TOTAL_SIZE")" | |
| fi | |
| print "" | |
| print "β Check complete!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment