批量打包镜像:
# docker save $(docker images | grep -v REPOSITORY | awk 'BEGIN{OFS=":";ORS=" "}{print $1,$2}') -o k8s-master.tar
导入镜像:
# docker load -i k8s-master.tar
-------------------------------------------------------------------------
#批量修改images的原有库上传到新库
#!/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
----------------------------------------------------------------
##批量save images镜像
#!/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
---------------------------------------------
##批量导入 images镜像
#!/bin/bash
for i in *tar
do sudo docker load -i $i
done
--------------------------------------------------------------------
###批量pull harbor库中的镜像
#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
-------------------------------------------------------------------
#批量为镜像打tag
#!/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
-------------------------------------------------------------------
#!/bin/bash
#备份kolla的镜像
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
1、删除所有容器
docker rm `docker ps -a -q`
2、删除所有镜像
docker rmi `docker images -q`
3、按条件删除镜像
docker rmi `docker images -q | awk '/^<none>/ { print $3 }'`
docker rmi --force `docker images | grep doss-api | awk '{print $3}'` //其中doss-api为关键字