Skip to content

Instantly share code, notes, and snippets.

@jessequinn
Forked from rluvaton/README.md
Created May 23, 2022 18:57
Show Gist options
  • Save jessequinn/0c9caa90722a368053e2bc0806f3a84c to your computer and use it in GitHub Desktop.
Save jessequinn/0c9caa90722a368053e2bc0806f3a84c to your computer and use it in GitHub Desktop.

Revisions

  1. @rluvaton rluvaton revised this gist Jan 18, 2021. 1 changed file with 4 additions and 5 deletions.
    9 changes: 4 additions & 5 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -4,14 +4,13 @@
    ## docker-load-and-push

    ### Install globaly
    ```bash
    ```bat
    sudo curl -o /usr/local/bin/docker-load-and-push https://gist.github.com/rluvaton/ef6415c79c35c5b11c535040e79ab43b/raw/19cb82651a10d5256f512331b484dee8a19a8058/docker-load-and-push.sh && sudo chmod +x /usr/local/bin/docker-load-and-push
    ```

    ### Usage

    ```
    $ docker-load-and-push --help
    ```console
    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:
    @@ -29,7 +28,7 @@ Example: ./docker-load-and-push.sh /mydir/ubuntu.tar --push --registry
    ## docker-save-and-retag

    ### Install globaly
    ```bash
    ```bat
    sudo curl -o /usr/local/bin/docker-save-and-retag https://gist.github.com/rluvaton/ef6415c79c35c5b11c535040e79ab43b/raw/19cb82651a10d5256f512331b484dee8a19a8058/docker-save-and-retag.sh && sudo chmod +x /usr/local/bin/ docker-save-and-retag
    ```

  2. @rluvaton rluvaton revised this gist Jan 18, 2021. 2 changed files with 161 additions and 25 deletions.
    45 changes: 45 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,45 @@
    # Docker images tar utils


    ## docker-load-and-push

    ### Install globaly
    ```bash
    sudo curl -o /usr/local/bin/docker-load-and-push https://gist.github.com/rluvaton/ef6415c79c35c5b11c535040e79ab43b/raw/19cb82651a10d5256f512331b484dee8a19a8058/docker-load-and-push.sh && sudo chmod +x /usr/local/bin/docker-load-and-push
    ```

    ### Usage

    ```
    $ 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
    ```bash
    sudo curl -o /usr/local/bin/docker-save-and-retag https://gist.github.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`

    141 changes: 116 additions & 25 deletions docker-load-and-push.sh
    Original file line number Diff line number Diff line change
    @@ -1,52 +1,143 @@
    #!/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 "
    }
    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
    display_usage
    print_error "### Docker images command return error ###"
    exit 1
    fi

    echo -e "\nLoading $1..."
    # 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

    # Load image and save output
    RESULT=$(docker load -i $1)

    # Get image name and registry
    IMAGE=${RESULT#*: }
    REGISTRY=${IMAGE#*\/}

    echo $RESULT
    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

    # Push if flag provided
    if [[ $* == *--push* ]]; then
    if [ -f "$DOCKER_TAR" ]; then
    load_docker_tar $DOCKER_TAR
    fi

    echo -e "\nPushing $IMAGE to $REGISTRY..."
    docker push $IMAGE
    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

    # 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
  3. @stefanvangastel stefanvangastel revised this gist Dec 16, 2016. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions docker-save-and-retag.sh
    Original file line number Diff line number Diff line change
    @@ -43,5 +43,10 @@ fi
    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
  4. @stefanvangastel stefanvangastel revised this gist Dec 16, 2016. 1 changed file with 52 additions and 0 deletions.
    52 changes: 52 additions & 0 deletions docker-load-and-push.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,52 @@
    #!/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
  5. @stefanvangastel stefanvangastel created this gist Dec 15, 2016.
    47 changes: 47 additions & 0 deletions docker-save-and-retag.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,47 @@
    #!/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