执行脚本
#!/bin/bash
declare -A SOURCE_TO_PROXY=(
["cr.l5d.io"]="l5d.m.daocloud.io"
["docker.elastic.co"]="elastic.m.daocloud.io"
["docker.io"]="docker.m.daocloud.io"
["gcr.io"]="gcr.m.daocloud.io"
["ghcr.io"]="ghcr.m.daocloud.io"
["k8s.gcr.io"]="k8s-gcr.m.daocloud.io"
["registry.k8s.io"]="k8s.m.daocloud.io"
["mcr.microsoft.com"]="mcr.m.daocloud.io"
["nvcr.io"]="nvcr.m.daocloud.io"
["quay.io"]="quay.m.daocloud.io"
["registry.jujucharms.com"]="jujucharms.m.daocloud.io"
["rocks.canonical.com"]="rocks-canonical.m.daocloud.io"
)
DOMAIN_REGEX="^[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$"
pull_and_push_image() {
local image=$1
local target_registry="registry.target.com"
local parts=(${image//\// })
if [[ ${#parts[@]} -gt 1 && ${parts[0]} =~ $DOMAIN_REGEX ]]; then
local source_registry=${parts[0]}
local image_name=$(echo "$image" | cut -d'/' -f2-)
else
local source_registry="docker.io"
local image_name=$image
image="$source_registry/library/$image_name"
fi
local proxy_registry=${SOURCE_TO_PROXY[$source_registry]}
if [ -z "$proxy_registry" ]; then
echo "未找到源站 $source_registry 的代理源站映射,使用原始镜像源站"
proxy_registry=$source_registry
fi
local proxy_image="$proxy_registry/$image_name"
echo "拉取代理源站的镜像 $proxy_image"
docker pull $proxy_image
if [ $? -ne 0 ]; then
echo "拉取镜像 $proxy_image 失败,跳过"
return
fi
local new_image="$target_registry/$image_name"
echo "为镜像 $proxy_image 打新标签 $new_image"
docker tag $proxy_image $new_image
echo "推送镜像 $new_image"
docker push $new_image
if [ $? -ne 0 ]; then
echo "推送镜像 $new_image 失败"
fi
}
if [ $# -ne 1 ]; then
echo "使用方法: $0 <镜像名>"
exit 1
fi
IMAGE=$1
pull_and_push_image $IMAGE
echo "镜像 $IMAGE 处理完成"