Last active
June 28, 2024 12:35
-
-
Save antoniopresto/50bb035d7e4bb55e00be959652c0c282 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
#!/bin/bash | |
# Check if an argument is provided | |
if [ "$#" -ne 1 ]; then | |
echo "Usage: $0 <path_to_Dockerfile | image_name>" | |
exit 1 | |
fi | |
# Function to list the contents of a Docker image in a tree format | |
list_image_contents() { | |
local image_id=$1 | |
local temp_dir=$(mktemp -d) | |
docker save "$image_id" -o "$temp_dir/image.tar" | |
tar -xf "$temp_dir/image.tar" -C "$temp_dir" | |
local layer=$(find "$temp_dir" -name layer.tar | head -n 1) | |
if [ -z "$layer" ]; then | |
echo "Error: No layer.tar found in the image." | |
rm -rf "$temp_dir" | |
exit 1 | |
fi | |
tree_output=$(tar -tf "$layer" | sed 's/[^-][^\/]*\//| /g;s/| \([^|]\)/|--- \1/') | |
if [ -z "$tree_output" ]; then | |
echo "Error: Unable to generate tree output." | |
rm -rf "$temp_dir" | |
exit 1 | |
fi | |
echo "$tree_output" | less | |
rm -rf "$temp_dir" | |
} | |
# Determine if the argument is a Dockerfile path or an image name | |
if [[ -f $1 ]]; then | |
image_name=$(docker build -q -f "$1" .) | |
list_image_contents "$image_name" | |
docker rmi "$image_name" | |
else | |
list_image_contents "$1" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment