Skip to content

Instantly share code, notes, and snippets.

@stefanvangastel
Last active March 9, 2023 23:19
Show Gist options
  • Save stefanvangastel/d6b5b38e8716ea102b651c67c100225f to your computer and use it in GitHub Desktop.
Save stefanvangastel/d6b5b38e8716ea102b651c67c100225f 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.
#!/bin/bash
#### Functions ###
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 <saved_image> [--push]\n"
echo -e " <saved_image>\t\t\tThe image file to load and push"
echo -e " [--push]\t\t\tPush to registry"
echo -e "\nExample: $0 /mydir/ubuntu.tar --push "
}
# Check params
if [ $# -le 0 ]
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
echo -e "\nLoading $1..."
# Load image and save output
RESULT=$(docker load -i $1)
# Get image name and registry
IMAGE=${RESULT#*: }
REGISTRY=${IMAGE#*\/}
echo $RESULT
# Push if flag provided
if [[ $* == *--push* ]]; then
echo -e "\nPushing $IMAGE to $REGISTRY..."
docker push $IMAGE
# Check result
if [[ $rc != 0 ]]; then
echo -e "\nERROR: Push failed, are you logged in to $REGISTRY? (e.g. \$ docker login $REGISTRY)"
exit 1
fi
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
@mifitous
Copy link

@stefanvangastel : please add in docker-save-and-retag.sh line 37
docker push $3
echo "Pushed to $3"

@stefanvangastel
Copy link
Author

@stefanvangastel : please add in docker-save-and-retag.sh line 37
docker push $3
echo "Pushed to $3"

I want to keep save and retag and load and push separated since I use this script to bridge an airgap.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment