批量修改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 "\033[34m執行命令pull: docker pull $to_whole \033[0m"
                                docker pull  $to_whole
                        echo "------------------------------------------------------------------------->"
                                echo -e "\033[32m執行命令tag: docker tag $to_whole $to_part${to_whole#*/*/} \033[0m"
                                docker tag $to_whole $to_part${to_whole#*/*/}
                        echo "------------------------------------------------------------------------->"
                                echo -e "\033[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 "\033[34m執行命令pull: docker pull $host \033[0m"
                                docker pull $host
                        echo "------------------------------------------------------------------------->"
                                echo -e "\033[32m執行命令tag: docker tag $host $to_all${host#*/*/} \033[0m"
                                docker tag $host $to_all${host#*/*/}
                        echo "------------------------------------------------------------------------->"
                                echo -e "\033[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 "\033[32m執行命令tag: docker tag $to_origin${n#*/*/} $target${n#*/*/} \033[0m"
                                docker tag $to_origin${n#*/*/} $target${n#*/*/}
                        echo "------------------------------------------------------------------------->"
                                echo -e "\033[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 "\033[34m執行命令pull: docker pull $host \033[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 "\033[33m執行命令push : docker push $host \033[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 "\033[33m執行命令push : docker push $local_images \033[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 "\033[32m正在執行打包命令請稍等.....\033[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 "\033[33m$tar_name.tar已打包完成.\033[0m"
                        else
                                echo -e "\033[31m 打包失敗請檢查問題。\033[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 "\033[33m正在執行刪除所有鏡像命令: docker rmi \033[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 "\033[33m正在刪除的鏡像:  $local \033[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 "\033[33m正在刪除容器了命令: docker rm  \033[0m"
                                docker rm `docker ps -a -q` -f
                        else
                                break
                        fi
        done
        ;;
        exit)
        break
        ;;
        *)
        echo -e "\033[35m輸入的不是規定的參數,取消執行命令!
(請輸入數字1或2操作指令pull/local,退出請輸入數字:" 3/exit ")\033[0m"
        echo ""
        echo "=====================================================>"
        esac
done

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM