Skip to content

Instantly share code, notes, and snippets.

@rluvaton
Forked from stefanvangastel/docker-load-and-push.sh
Last active August 26, 2022 12:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save rluvaton/ef6415c79c35c5b11c535040e79ab43b to your computer and use it in GitHub Desktop.
Save rluvaton/ef6415c79c35c5b11c535040e79ab43b to your computer and use it in GitHub Desktop.
Bash scripts to pull, (optional) retag, save, load and push Docker images. Created to provide easy means to download an image, retag it to use a private registry and then save it to an external disk. In a offline or on-premise environment you can use the load and push script to load images and push them to a private registry.

Docker images tar utils

docker-load-and-push

Install globaly

sudo curl -o /usr/local/bin/docker-load-and-push https://gist.githubusercontent.com/rluvaton/ef6415c79c35c5b11c535040e79ab43b/raw/19cb82651a10d5256f512331b484dee8a19a8058/docker-load-and-push.sh && sudo chmod +x /usr/local/bin/docker-load-and-push

Usage

foo@bar:~$ docker-load-and-push --help
This script must be run with Docker capable privileges and you should login to your registry before pushing!

Usage:
./docker-load-and-push.sh [--dir <dir_with_images>] [--file <saved_image>] [--registry <registry-url>] --auto-delete

 -h,--help                                      Display this help
 -f,--file <saved_image>                        The image file to load and push
 -d,--dir <dir_with_images>                     The directory containing images file to load and push
 [--registry <registry-url>]                    Push to specific registry
 --auto-delete                                  Delete tar file after upload

Example: ./docker-load-and-push.sh /mydir/ubuntu.tar --push --registry

docker-save-and-retag

Install globaly

sudo curl -o /usr/local/bin/docker-save-and-retag https://gist.githubusercontent.com/rluvaton/ef6415c79c35c5b11c535040e79ab43b/raw/19cb82651a10d5256f512331b484dee8a19a8058/docker-save-and-retag.sh && sudo chmod +x /usr/local/bin/ docker-save-and-retag

Usages

Then use docker-save-and-retag instead of ./docker-save-and-retag.sh in examples

Example: Basic usage docker-save-and-retag.sh ./docker-save-and-retag.sh ubuntu:latest /mydir/ubuntu.tar my.private.registry/docker/ubuntu:latest

Example: Save and retag all local images as escaped_image_name.tar in /path/to/ docker images --format "{{.Repository}}:{{.Tag}}" | while read line ; do ./docker-save-and-retag.sh $line "/path/to/$(echo $line | sed -r 's/[\:\/]+/_/g').tar" "my.private.registry.com/$line" ; done

#!/bin/bash
display_usage() {
echo "This script must be run with Docker capable privileges and you should login to your registry before pushing!"
echo -e "\nUsage:\n$0 [--dir <dir_with_images>] [--file <saved_image>] [--registry <registry-url>] --auto-delete\n"
echo -e " -h,--help\t\t\t\t\tDisplay this help"
echo -e " -f,--file <saved_image>\t\t\tThe image file to load and push"
echo -e " -d,--dir <dir_with_images>\t\t\tThe directory containing images file to load and push"
echo -e " [--registry <registry-url>]\t\t\tPush to specific registry"
echo -e " --auto-delete\t\t\t\t\tDelete tar file after upload"
echo -e "\nExample: $0 /mydir/ubuntu.tar --push --registry"
}
print_error() {
echo -e "\e[1;31mError: $@\e[0m"
}
print_warning() {
echo -e "\e[1;33m$@\e[0m"
}
ctrl_c_handler() {
print_warning "CTRL-C detected, aborting..."
exit 1
}
delete_file() {
local docker_tar=$1
echo -e "\nDeleting $docker_tar..."
rm $docker_tar
echo -e "\nDeleted"
}
load_docker_tar() {
local docker_tar=$1
echo -e "\nLoading $docker_tar..."
# Load image and save output
RESULT=$(docker load -i $docker_tar)
# Get image name and registry
IMAGE=${RESULT#*: }
REGISTRY=${IMAGE#*\/}
# Push if flag provided
if [ ! -z "$REGISTRY_URL" ]; then
echo -e "\nPushing $IMAGE to $REGISTRY_URL..."
docker tag $IMAGE $REGISTRY_URL/$IMAGE
docker push $REGISTRY_URL/$IMAGE
echo -e "\nPushed"
# Check result
if [[ $rc != 0 ]]; then
print_error "\nERROR: Push failed, are you logged in to $REGISTRY? (e.g. \$ docker login $REGISTRY)"
exit 1
fi
fi
if [[ $AUTO_DELETE == 0 ]]; then
delete_file $docker_tar
fi
}
# Listen to CTRL-C
trap ctrl_c_handler 2
trap ctrl_c_handler SIGINT
# Check params
if [ $# -le 0 ]
then
print_warning "### no params specified ###"
display_usage
exit 1
fi
# Check Docker command executable exit code
docker images > /dev/null 2>&1; rc=$?;
if [[ $rc != 0 ]]; then
print_error "### Docker images command return error ###"
exit 1
fi
# Get path arguments
while [ $# -gt 0 ]; do
case "$1" in
--file*|-f*)
if [[ "$1" != *=* ]]; then shift;
fi # Value is next arg if no `=` (equal sign)
DOCKER_TAR="${1#*=}"
;;
--dir*|-d*)
if [[ "$1" != *=* ]]; then shift;
fi # Value is next arg if no `=` (equal sign)
FOLDER_WITH_DOCKER_TARS="${1#*=}"
;;
--registry*)
if [[ "$1" != *=* ]]; then shift;
fi # Value is next arg if no `=` (equal sign)
REGISTRY_URL="${1#*=}"
;;
--auto-delete)
AUTO_DELETE=0
;;
--help|-h)
display_usage
exit 0
;;
*)
>&2 printf "Error: Invalid argument $@\n"
exit 1
;;
esac
shift
done
if [ ! -f "$DOCKER_TAR" ] && [ ! -d "$FOLDER_WITH_DOCKER_TARS" ] && [[ ! -n "$FOLDER_WITH_DOCKER_TARS" ]]; then
print_error "The file $DOCKER_TAR or the folder $FOLDER_WITH_DOCKER_TARS doesn't exist"
exit 1
fi
if [ -f "$DOCKER_TAR" ]; then
load_docker_tar $DOCKER_TAR
fi
if [ -d "$FOLDER_WITH_DOCKER_TARS" ] && [[ -n "$FOLDER_WITH_DOCKER_TARS" ]]; then
for docker_tar_file in $FOLDER_WITH_DOCKER_TARS/*.tar; do
load_docker_tar "$docker_tar_file"
done
fi
echo "Done!"
exit 0
#!/bin/bash
#### Functions ###
display_usage() {
echo "This script must be run with Docker capable privileges!"
echo -e "\nUsage:\n$0 <image> <save_to_file> [retag_name] \n"
echo -e " <image>\t\t\tThe image to pull"
echo -e " <save_to_file>\t\t\tFilename to save the image to"
echo -e " [retag_name]\t\t\t(Optional) new name (tag) for image"
echo -e "\nExample: $0 mysql/mysql-server:latest /mydir/mysql.tar my.private.registry/mysql/mysql-server:latest"
}
# Check params
if [ $# -le 1 ]
then
display_usage
exit 1
fi
# Check Docker command executable exit code
docker images > /dev/null 2>&1; rc=$?;
if [[ $rc != 0 ]]; then
display_usage
exit 1
fi
# Pull image
docker pull $1
# Set image name to save
IMAGE=$1
# Retag image if retag name give
if [ ! -z "$3" ]; then
docker tag $1 $3
echo "Retaged $1 to $3"
# Overwrite image to save
IMAGE=$3
fi
# Save to output file
docker save -o $2 $IMAGE
echo "Saved $IMAGE to $2"
# Untag image if retag name give
if [ ! -z "$3" ]; then
docker rm $IMAGE
fi
echo "Done!"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment