镜像的导入和导出

1、批量打包镜像

1
docker save $(docker images | grep -v REPOSITORY | awk  'BEGIN{OFS=":";ORS="\n"}{print $1,$2}') -o k8s-master.tar

除了awk常用于拼接镜像名和版本号,更快地方法

1
2
3
4
docker images --format  "{{.ID}}:{{.Repository}}"
 
docker inspect --format='{{index .RepoTags 0}}' d2d4688fcebe
redis:alpine

2、导入镜像:

1
docker load -i k8s-master.tar

3、批量修改images的原有库上传到新库

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
------------------------------------------------------------------
#!/bin/bash
 
readonly old_repo=harbor.cetccloud.com/amd64
readonly new_repo=harbor.cetccloud.com/amd64
 
for image  in $(docker images --format  '{{.Repository}}:{{.Tag}}'); do
    name=${image##*/}
    new_img=${new_repo}/${name}
    echo "Processing ${image} -> ${new_img}"
    sudo docker tag ${image} ${new_img}
    sudo docker push ${new_img}
    sudo docker rmi ${new_img}
done
----------------------------------------------------------------

4、批量save images镜像

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/bin/bash
#Output the information of images to images.txt
sudo docker image ls >images.txt
#Get the number of images according to the total of content's rows
N=$(awk 'END{print NR}' images.txt)
echo The total of images is $N
#Get names and tags of images
for ((i=2; i<=$N; i++))
  do
    image=$(awk 'NR=="'"$i"'" {print $1}' images.txt)
    version=$(awk 'NR=="'"$i"'" {print $2}' images.txt)
#Modify the name of files which will be saved in local , otherwise you will be failed to save images.
    shortname=`echo $image | sed 's/.*\///g'`
#filename is name of the file that will be saved
    filename="${shortname}-${version}.tar"
    imagename="${image}:${version}"
 
#Output the value of variable
    echo image=$image
    echo version=$version
    echo filename=$filename
    echo imagename=$imagename
    echo shortname=$shortname
 
#Save the image as a local tar file
    sudo docker save -o $filename $imagename
#Modify the permission of the file
    sudo chmod 755 $filename
#Output the result!
    echo No.$i $shortname is saved successfully!
  done
#Delete file
rm images.txt
---------------------------------------------

5、批量pull harbor库中的镜像

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#curl -X GET --header 'Accept: application/json' 'https://harbor.cetccloud.com/api/search?q=amd64'
目的是获取amd64的project_id=多少,替换到脚本里面
amd64可以换harbor里其他仓库名
 
#!/bin/bash
URL="https://harbor.cetccloud.com"
IP="harbor.cetccloud.com"
USER="deploy"
PASS="Wg7FPtHbAB"
REPOS=$(curl -s -X GET --header 'Accept: application/json' "${URL}/api/repositories?project_id=9"|grep "name"|awk -F '"' '{print $4}')
for rp in ${REPOS}
do
  TAGS=$(curl -s -X GET --header 'Accept: application/json' "${URL}/api/repositories/${rp}/tags"|grep \"name\"|awk -F '"' '{print $4}'|sort -r)
  a=$(echo ${rp}|awk -F "/" '{print $2}')
    for t in ${TAGS}
    do
        docker pull ${IP}"/"${rp}":"${t}
        echo ${IP}"/"${rp}":"${t} >> /opt/docker.tag
        docker save ${IP}"/"${rp}":"${t} > /opt/docker/${IP}-${a}-${t}.tar.gz
    done
    echo "===================="
done
-------------------------------------------------------------------

6、批量为镜像打tag

1
2
3
4
5
6
7
8
9
10
11
12
#!/bin/bash
 
readonly old_repo=192.168.10.15:8012/amd64
readonly new_repo=harbor.cetccloud.com/amd64
 
for image in $(docker images --format '{{.Repository}}:{{.Tag}}'); do
    name=${image##*/}
    new_img=${new_repo}/${name}
    echo "Processing ${image} -> ${new_img}"
    sudo docker tag ${image} ${new_img}
    sudo docker rmi ${image}
done

7、备份kolla的镜像

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
-------------------------------------------------------------------
#!/bin/bash
LIST=" "  #可在外面执行脚本的时候带上参数
TXT=/root/tmp.txt
BAKDIR=/usr/local/bak
LOGDIR=/usr/local/bak/log
LOGFILE=$LOGDIR/`date +%Y%m%d`.log
 
[ ! -d $BAKDIR ] && mkdir -p $BAKDIR
[ ! -d $LOGDIR ] && mkdir -p $LOGDIR
 
#此部分为用户自定义备份镜像(即部分备份)
if [ ! -n "$LIST" ]
then
        for list in $LIST
        do
                RESLIST=`docker images |grep $list | awk '{print $1}'`
                for reslist in $RESLIST
                do
                RESTAG=`docker images |grep "$reslist" |awk '{a=$1":"$2;print a }'`
                BAKNAME=`docker images |grep "$reslist" |awk '{a=$1":"$2;print a }'|sed 's/\//_/g'`
                /usr/bin/docker save $RESTAG -o $BAKDIR/$BAKNAME.tar  >> $LOGFILE 2>&1
                done
        done
 
#全备部分
else
        RTI=`docker images |awk '{print $1,$2,$3}'|sed 1d > $TXT`
#RTI表示仓库名、tag名、imagesID,sed 1d 表示删除第一行
 
        RESLIST=`cat $TXT|awk '{print $1}'`
 
#RESLIST表示镜像的名字(包含仓库路径)
        for reslist in $RESLIST
        do
                RESTAG=`docker images |grep "$reslist" |awk '{a=$1":"$2;print a }'`
                BAKNAME=`docker images |grep "$reslist" |awk '{a=$1":"$2;print a }'|sed 's/\//_/g'`
                docker save $RESTAG -o $BAKDIR/$BAKNAME.tar  >> $LOGFILE 2>&1
        done
        /usr/bin/rm -f $TXT
fi

8、按条件删除镜像

1
2
docker rmi `docker images -q | awk '/^<none>/ { print $3 }'`
docker rmi --force `docker images | grep doss-api | awk '{print $3}'`    //其中doss-api为关键字

9、恢复备份镜像到registry

  

  

 

 

 

https://mp.weixin.qq.com/s/OAi3lXZ5jpLEIoK2sDxizw

posted @   凡人半睁眼  阅读(623)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· 写一个简单的SQL生成工具
· Manus的开源复刻OpenManus初探

阅读目录(Content)

此页目录为空

点击右上角即可分享
微信分享提示