docker镜像导出到指定服务

131 阅读1分钟

我的虚机没有保存全部镜像的空间,想要把镜像全部同步到另一个服务,只能按镜像名导出tar文件后,scp到另外的虚机里

两个虚机要好ssh免密登录

#!/bin/bash

local_images=$(docker images|awk '{position=$1":"$2; print position}')


for image_with_tag in ${local_images[@]}
do
	if [[ $image_with_tag != 'REPOSITORY:TAG' ]]
	then
		echo "处理: ${image_with_tag}"
		image=$(echo $image_with_tag|sed 's/\./_/g' | sed 's/\//_/g')
		docker save -o $image.tar $image_with_tag
		scp ./$image.tar root@172.31.51.101:~/images
		ssh root@172.31.51.101 "cd /root/images;  docker load -i $image.tar; rm -f $image.tar"
		rm -f ./$image.tar
		echo "处理完成"
	fi
done