Skip to content

Instantly share code, notes, and snippets.

@alexkubica
Created July 29, 2018 07:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexkubica/0184811d265419fe2e6e0354432116ee to your computer and use it in GitHub Desktop.
Save alexkubica/0184811d265419fe2e6e0354432116ee to your computer and use it in GitHub Desktop.
Shell script to download docker images
#!/bin/bash
set -xe
# Prints usage message
BASENAME="$0"
usage ()
{
echo "Usage : (basename "$BASENAME") [options]"
echo ""
echo "Description: Downloads artifacts from maven central repository with sources and javadocs"
echo ""
echo "Options:"
echo "-d,--dest <arg> Destination for downloaded images. Should be without "
echo " a trailing slash and must exist, defaults to 'whiten'"
echo "-i,--image <arg> Image's identifier"
echo "-f,--file <arg> Get image identifiers list from file"
echo "-h,--help Display help information"
}
download_image()
{
docker pull $2
# Remvoe invalid characters from image name
filtered_file_name=$(echo $2 | sed 's/[^A-Za-z0-9.]/-/g')
docker save -o "${1}/${filtered_file_name}.tar" $2
}
# Get arguments
DEST='whiten'
while [ "$1" != "" ]; do
case $1 in
-i | --image ) shift
IMAGE=$1
;;
-f | --file ) shift
FILE=$1
;;
-d | --dest ) shift
DEST=$1
;;
-h | --help ) usage
exit
;;
* ) usage
exit 1
esac
shift
done
# Validate arguments
if [ "$DEST" = "" ]; then
usage
exit 1
fi
if [ "$IMAGE" = "" ]; then
if [ "$FILE" = "" ]; then
usage
exit 1
else
while read -r line || [[ -n "$line" ]]; do
download_image $DEST $line
done < "$FILE"
fi
else
download_image $DEST $IMAGE
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment