-
-
Save jessequinn/0c9caa90722a368053e2bc0806f3a84c 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 hidden or 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" | |
| echo "Done!" | |
| exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment