批量修改docker镜像脚本

此脚本可以修改单个镜像,pull ,tag,push 功能,也可以全量修改本地镜像和拉取全量镜像并修改等。具体执行该脚本并查看说明。

  

#!/bin/bash
set -e
PS3="选择要执行的命令按数字:
1)拉取单个新的镜像并tag后推送至新镜像仓库。
2)拉取全部新的镜像并tag后推送需要在本地创建一个*.pi结尾的文件并把需要的镜像按列表黏贴进去即可(注意需要放到和脚本同级目录下)。
3)更改本地已有镜像tag后并推送至新镜像仓库。
4)只拉取镜像(复用2选项,或创建一个*.pi结尾的文件)。
5)只推送镜像(复用2选项,或创建一个*.pi结尾的文件)。
6)批量打包镜像。
7)删除镜像或容器。
(退出请按 8)exit 或 Ctrl+c):"
select to_chose in once all local pull push tar del exit;do


        case $to_chose in
        once)
        while true;do
                read -ep "输入镜像全路径包括tag,例如: github.com/test/abc:v0.0.1(回到上一级输入"exit"):"  to_whole
                        if [[ $to_whole == exit ]];then
                        break
                        fi
                read -ep "输入目标镜像部分路径,例如: myhub.com/test/(末尾必须加'/'斜杠)(回到上一级输入"exit"):" to_part
                        if [[ $to_part == exit ]];then
                        break
                        fi
                        echo "------------------------------------------------------------------------->"
                                echo -e "33[34m执行命令pull: docker pull $to_whole 33[0m"
                                docker pull  $to_whole
                        echo "------------------------------------------------------------------------->"
                                echo -e "33[32m执行命令tag: docker tag $to_whole $to_part${to_whole#*/*/} 33[0m"
                                docker tag $to_whole $to_part${to_whole#*/*/}
                        echo "------------------------------------------------------------------------->"
                                echo -e "33[33m执行命令push: docker push $to_part${to_whole#*/*/} 033[0m"
                                docker push $to_part${to_whole#*/*/}
        done
        ;;
        all)
        while true;do
          hosts=`sed -n '/^[^#]/p' *.pi`
                read -ep "输入目标镜像部分路径,例如: myhub.com/test/(末尾必须加'/'斜杠)(回到上一级输入"exit"):" to_all
                        if [[ $to_whole == exit ]];then
                        break
                        fi
            for host in $hosts;do
                        echo "------------------------------------------------------------------------->"
                                echo -e "33[34m执行命令pull: docker pull $host 33[0m"
                                docker pull $host
                        echo "------------------------------------------------------------------------->"
                                echo -e "33[32m执行命令tag: docker tag $host $to_all${host#*/*/} 033[0m"
                                docker tag $host $to_all${host#*/*/}
                        echo "------------------------------------------------------------------------->"
                                echo -e "33[33m执行命令push: docker push $to_all${host#*/*/} 033[0m"
                                docker push $to_all${host#*/*/}
                done
        done
        ;;
        local)
        while true;do
                read -ep "输入原镜像部分路径例如: github.com/test/:(末尾必须加'/'斜杠)(回到上一级输入"exit"):"  to_origin
                        if [[ $to_origin == exit ]];then
                        break
                        fi
                read -ep "输入目标镜像部分路径例如: myhub.com/test/(末尾必须加'/'斜杠)(回到上一级输入"exit"):" to_target
                        if [[ $to_target == exit ]];then
                        break
                        fi
                origin=`docker images | grep "$to_origin"| grep -v REPOSITORY | awk 'BEGIN{OFS=":"}{print $1,$2}'`
                target=$to_target
                for n in ${origin[@]}
                do
                if [ "${n#*/*/}" == "${n#*/*/}" ];then
                        echo "------------------------------------------------------------------------->"
                                echo -e "33[32m执行命令tag: docker tag $to_origin${n#*/*/} $target${n#*/*/} 33[0m"
                                docker tag $to_origin${n#*/*/} $target${n#*/*/}
                        echo "------------------------------------------------------------------------->"
                                echo -e "33[33m执行命令push: docker push $target${n#*/*/} 033[0m"
                                docker push $target${n#*/*/}
                else
                        continue
                fi
                done
        done
        ;;
        pull)
        while true;do
          hosts=`sed -n '/^[^#]/p' *.pi`
                read -ep "按回车继续,(回到上一级输入"exit"):" to_pull
                        if [[ $to_pull == exit ]];then
                        break
                        fi
                for host in $hosts;do
                        echo "------------------------------------------------------------------------->"
                                echo -e "33[34m执行命令pull: docker pull $host 33[0m"
                                docker pull $host
                done
        done
        ;;
        push)
        while true;do
          hosts=`sed -n '/^[^#]/p' *.pi`
                read -ep "是否推送本地*.pi列表镜像,是输入:'y' ,或输入:'n' (推送本地仓库或单个镜像)继续(回到上一级输入"exit"):" to_push
                if [[ $to_push == exit ]];then
                break
                fi
                [[ $to_push =~ ^[y,n]$ ]]  || { echo "请输入'y' 或者'n' 继续" ; continue ; }
                if [[ $to_push == y ]];then
                for host in $hosts;do
                        echo "------------------------------------------------------------------------->"
                                echo -e "33[33m执行命令push : docker push $host 33[0m"
                                docker push $host
                done
                elif  [[ $to_push == n ]];then
                read -ep "输入本地镜像部分路径(只推送单个镜像请输入全路径)例如:(部分路径会推送全部)myhub.com/test/(末尾必须加'/'斜杠)或(全路径)myhub.com/test:latest(回到上一级输入"exit"):" to_local
                        if [[ $to_local == exit ]];then
                        break
                        fi
                local=`docker images | grep "$to_local"| grep -v REPOSITORY | awk 'BEGIN{OFS=":"}{print $1,$2}'`
                for local_images in $to_local;do
                        echo "------------------------------------------------------------------------->"
                                echo -e "33[33m执行命令push : docker push $local_images 33[0m"
                                docker push $local_images
                done
                else
                   break
                fi
        done
        ;;
        tar)
        while true;do
                read -ep "输入要打包镜像的部分路径,例如: github.com/test/:(末尾必须加'/'斜杠)(回到上一级输入"exit"):"  images_route
                        if [[ $images_route == exit ]];then
                        break
                        fi
                read -ep "输入打包名字并回车,导入提示:(docker load -i tar_name.tar)(回到上一级输入"exit"):"  tar_name
                        if [[ $tar_name == exit ]];then
                        break
                        fi
                        echo "------------------------------------------------------------------------->"
                                echo -e "33[32m正在执行打包命令请稍等.....33[0m"
                                docker save $(docker images |grep "$images_route" | grep -v REPOSITORY | awk 'BEGIN{OFS=":";ORS=" "}{print $1,$2}') -o $tar_name.tar
                        if [[ $? = 0 ]];then
                                echo -e "33[33m$tar_name.tar已打包完成.33[0m"
                        else
                                echo -e "33[31m 打包失败请检查问题。33[0m"
                        fi
        done
        ;;
        del)
        while true;do
                read -ep "删除本地所有镜像或部分镜像: 删除全部按'1',部分按'2',删除所有容器按'3'(回到上一级输入"exit"):" del_images
                        if [[ $del_images == exit ]];then
                        break
                        fi
                        [[ $del_images =~ ^[1,2,3]$ ]]  || { echo "请输入'1' 或'2' 或者'3' 继续" ; continue ; }
                        if [[ $del_images == "1" ]];then
                        echo "------------------------------------------------------------------------->"
                                echo -e "33[33m正在执行删除所有镜像命令: docker rmi 33[0m"
                                docker rmi `docker images -a -q` -f
                        elif [[ $del_images == "2" ]];then
                read -ep "输入本地镜像部分路径例如: myhub.com/test/(末尾必须加'/'斜杠)(回到上一级输入"exit"):" part_images
                        if [[ $part_images == exit ]];then
                        break
                        fi
                        local=`docker images | grep "$part_images"| grep -v REPOSITORY | awk 'BEGIN{OFS=":"}{print $1,$2}'`
                        echo "------------------------------------------------------------------------->"
                                echo -e "33[33m正在删除的镜像:  $local 33[0m"
                        for images in $local;do
                                docker rmi $images
                        done
                        elif [[ $del_images == "3" ]];then
                read -ep "正要执行删除所有镜像,确认按回车 (回到上一级输入"exit"):" del_container
                        if [[ $del_container == exit ]];then
                        break
                        fi
                        echo "------------------------------------------------------------------------->"
                                echo -e "33[33m正在删除容器了命令: docker rm  33[0m"
                                docker rm `docker ps -a -q` -f
                        else
                                break
                        fi
        done
        ;;
        exit)
        break
        ;;
        *)
        echo -e "33[35m输入的不是规定的参数,取消执行命令!
(请输入数字1或2操作指令pull/local,退出请输入数字:" 3/exit ")33[0m"
        echo ""
        echo "=====================================================>"
        esac
done
原文地址:https://www.cnblogs.com/sunju/p/15459592.html