Last active
March 9, 2023 23:19
-
-
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.
This file contains 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 | |
#### 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 |
This file contains 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 | |
#### 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 |
@stefanvangastel I updated this gist to work with a custom registry + import all images in a provided directory. My fork
@stefanvangastel I updated this gist to work with a custom registry + import all images in a provided directory. My fork
Awesome!
@stefanvangastel : please add in docker-save-and-retag.sh line 37
docker push $3
echo "Pushed to $3"
@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
Install global
sudo wget -O /usr/local/bin/docker-save-and-retag https://gist.github.com/stefanvangastel/d6b5b38e8716ea102b651c67c100225f/raw/b0b13deb46e9cae07458dcb66160c1e5994fd43c/docker-save-and-retag.sh && sudo chmod +x /usr/local/bin/docker-save-and-retag
Then use
docker-save-and-retag
instead of./docker-save-and-retag.sh
in examplesExample: 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
Example: Load and push all saved images in
/path/to/*.tar
for file in /path/to/*.tar; do ./docker-load-and-push.sh $file --push; done