镜像的导入和导出
1、批量打包镜像
docker save $(docker images | grep -v REPOSITORY | awk 'BEGIN{OFS=":";ORS="\n"}{print $1,$2}') -o k8s-master.tar
除了awk常用于拼接镜像名和版本号,更快地方法
docker images --format "{{.ID}}:{{.Repository}}"
docker inspect --format='{{index .RepoTags 0}}' d2d4688fcebe
redis:alpine
2、导入镜像:
docker load -i k8s-master.tar
3、批量修改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
----------------------------------------------------------------
4、批量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
---------------------------------------------
5、批量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
-------------------------------------------------------------------
6、批量为镜像打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
7、备份kolla的镜像
-------------------------------------------------------------------
#!/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、按条件删除镜像
docker rmi `docker images -q | awk '/^<none>/ { print $3 }'`
docker rmi --force `docker images | grep doss-api | awk '{print $3}'` //其中doss-api为关键字
9、恢复备份镜像到registry
#!/bin/bash
# Docker registry address
DOCKER_REGISTRY=${curr_node_ip}:5000
# Backup file path
BACKUP_DIR=/mnt/backup_data/${archive_node}/docker-image
# Sort the directory by name and take the latest directory
for date_folder in $(ls -r $BACKUP_DIR | head -1)
do
# Traverse the image folder under the date folder
for image_folder in $(ls -t $BACKUP_DIR/$date_folder)
do
# Traverse the tar package under the image folder
for file in $(ls -t $BACKUP_DIR/$date_folder/$image_folder/*.tgz)
do
# Load docker image and get ID
ima=$file
output=$(docker load -i $ima)
loaded_image_prefix="Loaded image: "
if [[ $output =~ $loaded_image_prefix ]]
then
image_name_tag=${output#$loaded_image_prefix}
IMAGE_ID=$(docker images --format '{{.ID}}' $image_name_tag)
else
IMAGE_ID=$(echo $output | awk '{print $3}')
fi
if [ $IMAGE_ID != 'null' ]
then
# Get tag of docker image
IMAGE_TAG=$(docker inspect --format='{{index .RepoTags 0}}' $IMAGE_ID)
# If the image has a tag, then tag and push it
if [ ! -z "$IMAGE_TAG" ]
then
old_name=$IMAGE_TAG
new_name=""
# If old_name already contains DOCKER_REGISTRY, then DOCKER_REGISTRY is not added again
if [[ $old_name == $DOCKER_REGISTRY* ]]; then
new_name=$old_name
elif [[ $old_name == $mysqlm_ip:* ]]; then
new_name=$curr_node_ip:5000/$image_name_tag
else
new_name=$DOCKER_REGISTRY/$old_name
fi
docker tag $old_name $new_name
docker push $new_name
docker rmi $old_name
docker rmi $new_name
else
echo "Image $IMAGE_ID has no tag, skipping."
fi
else
echo "Failed to load image from $file"
fi
done
done
done
if [ ! -z "$(docker images -q -f dangling=true)" ]
then
docker rmi $(docker images -q -f dangling=true)
fi
https://mp.weixin.qq.com/s/OAi3lXZ5jpLEIoK2sDxizw

浙公网安备 33010602011771号