#!/bin/bash -e
# 检查是否传递了目标参数
if [ $# -ne 1 ]; then
echo "Usage: $0 <docker image>"
exit
fi
IMG=$1
###############################################################################
# DockerHub 镜像源列表
DOCKERHUB_REGISTRY_MIRRORS=(
"
docker.1panel.live"
"
docker.unsee.tech"
"
docker.m.daocloud.io"
"
docker.kejilion.pro"
"
registry.dockermirror.com"
"
hub.rat.dev"
"
dhub.kubesre.xyz"
"
docker.nastool.de"
)
###############################################################################
# 检查本地是否已经存在 ${IMG}
if docker images --format "{{.Repository}}:{{.Tag}}" | grep -q "^${IMG}$"; then
echo "镜像 ${IMG} 已经存在,跳过拉取步骤。"
exit 0
fi
# 遍历镜像源,拉取并打标签
for registry in "${DOCKERHUB_REGISTRY_MIRRORS[@]}"; do
registry_img=${registry}/${IMG}
# 检查本地是否已经存在 ${registry_img}
if docker images --format "{{.Repository}}:{{.Tag}}" | grep -q "^${registry_img}$"; then
echo "本地已经存在 ${registry_img},跳过拉取,进行打标签。"
# 为本地已有的镜像打标签为 ${IMG}
if docker tag ${registry_img} ${IMG}; then
echo "镜像 ${registry_img} 成功打标签为 ${IMG}。"
docker rmi ${registry_img} # 删除原镜像( registry 镜像源)
exit 0 # 成功后退出脚本
else
echo "为镜像 ${registry_img} 打标签失败!"
continue
fi
fi
echo "尝试从镜像源 ${registry} 拉取 ${IMG}..."
# 拉取镜像
if docker pull ${registry_img}; then
# 打标签为 ${IMG}
if docker tag ${registry_img} ${IMG}; then
echo "镜像 ${registry_img} 成功打标签为 ${IMG}。"
docker rmi ${registry_img} # 删除原镜像( registry 镜像源)
exit 0 # 成功后退出脚本
else
echo "为镜像 ${IMG} 打标签失败!"
docker rmi ${registry_img} # 删除失败的镜像
continue
fi
else
echo -e "从 ${registry} 拉取镜像失败,尝试下一个镜像源...\n"
continue
fi
done
echo "所有镜像源都尝试过,未能拉取并打标签 ${IMG}。"
# 保存为
docker-pull.sh ,
docker-pull.sh 镜像