参考 Rancher 离线安装脚本,对脚本进行简化。
功能:通过脚本可以从一个镜像仓库(例如 harbor)导出,然后导入到另一个镜像仓库。
首先需要整理要导出的镜像列表,写入 docker-images.txt
中,示例如下:
10.10.10.233/develop/assets:201912161902
10.10.10.233/develop/announcement-service:1.0.0-201911121013
10.10.10.233/develop/batch2-service:2.1.0-201912101825
10.10.10.233/develop/bizcardmanage-service:1.0.0-201910211755
10.10.10.233/develop/cart2-service:2.0.0-201911121022
10.10.10.233/develop/checkup2-service:2.1.0-201911111328
10.10.10.233/develop/cloud-web:1.0.0-201912181654
10.10.10.233/develop/comment-service:1.0.0-201909230900
10.10.10.233/develop/consumer-service:1.2.0-201911111333
10.10.10.233/develop/contract2-service:2.1.0-201912031522
10.10.10.233/develop/course-service:1.0.0-201911121026
10.10.10.233/develop/crm-service:8.2.0-201912121323
10.10.10.233/develop/document-service:1.0.0-201912111648
10.10.10.233/develop/employeemanage-service:1.1.0-201912161113
10.10.10.233/develop/employerarchive-service:1.1.0-201910251516
10.10.10.233/develop/files-service:1.0.0-201912181034
10.10.10.233/develop/finance-service:1.0.0-201912181634
10.10.10.233/develop/goods2-service:2.0.0-201912181432
然后使用下面的导出脚本,将上述所有镜像导出并打包为 docker-images.tar.gz
:
#!/bin/bash
list="docker-images.txt"
images="docker-images.tar.gz"
usage () {
echo "USAGE: $0
echo " [-l|--image-list path] text file with list of images; one image per line."
echo " [-i|--images path] tar.gz generated by docker save."
echo " [-h|--help] Usage message"
}
POSITIONAL=()
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
-i|--images)
images="$2"
shift # past argument
shift # past value
;;
-l|--image-list)
list="$2"
shift # past argument
shift # past value
;;
-h|--help)
help="true"
shift
;;
*)
usage
exit 1
;;
esac
done
if [[ $help ]]; then
usage
exit 0
fi
pulled=""
while IFS= read -r i; do
[ -z "${i}" ] && continue
if docker pull "${i}" > /dev/null 2>&1; then
echo "Image pull success: ${i}"
pulled="${pulled} ${i}"
else
if docker inspect "${i}" > /dev/null 2>&1; then
pulled="${pulled} ${i}"
else
echo "Image pull failed: ${i}"
fi
fi
done < "${list}"
echo "Creating ${images} with $(echo ${pulled} | wc -w | tr -d '[:space:]')
docker save $(echo ${pulled}) | gzip --stdout > ${images}
将导出的镜像拷到目标服务器后,执行下面的脚本进行导入:
#!/bin/bash
#注意修改下面的仓库地址
reg="10.10.1.5:10080"
lib="erp"
images="docker-images.tar.gz"
list="docker-images.txt"
usage () {
echo "USAGE: $0
echo " [-l|--image-list path] text file with list of images; one image per line."
echo " [-i|--images path] tar.gz generated by docker save."
echo " [-h|--help] Usage message"
}
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
-l|--image-list)
list="$2"
shift # past argument
shift # past value
;;
-i|--images)
images="$2"
shift # past argument
shift # past value
;;
-h|--help)
help="true"
shift
;;
*)
usage
exit 1
;;
esac
done
if [[ $help ]]; then
usage
exit 0
fi
docker load --input ${images}
load_images=()
while IFS= read -r i; do
[ -z "${i}" ] && continue
load_images+=("${i}");
done < "${list}"
for i in "${load_images[@]}"; do
[ -z "${i}" ] && continue
image_name="${reg}/${lib}/${i##*/}"
docker tag "${i}" "${image_name}"
docker push "${image_name}"
done
特别注意脚本中的 reg="10.10.1.5:10080"
改为要导入的镜像仓库地址。