Skip to content

Instantly share code, notes, and snippets.

@muhamadazmy
Created February 23, 2023 15:56
Show Gist options
  • Save muhamadazmy/697143782cc4ec77d9766580eb817977 to your computer and use it in GitHub Desktop.
Save muhamadazmy/697143782cc4ec77d9766580eb817977 to your computer and use it in GitHub Desktop.
Extract a docker image into a directory, does not require a container
set -e
# you need to run `docker save <image> -o <file>.tar
# then run this script like `extrat-image.sh <file>.tar`
# it will create a directoyr `<file>.tar-root` which containers
# all files from the image extracted
input=$1
if [ ! -f "${input}" ]; then
echo "file does not exist"
exit 1
fi
tmpdir=".${input}"
rootfs="${input}-root"
rm -rf $tmpdir || true
rm -rf $tmpdir || true
function finish {
rm -rf ${tmpdir} || true
}
trap finish EXIT
mkdir -p ${rootfs}
mkdir -p ${tmpdir}
tar -xf $input -C $tmpdir
mf=${tmpdir}/manifest.json
if [ ! -f "${mf}" ]; then
echo "invalid tar archive (no manifest.json file)"
exit 1
fi
for layer in $(jq '.[0].Layers[]' $mf); do
layer=${layer//\"/}
tar -xf "${tmpdir}/${layer}" -C $rootfs
done
echo "done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment