脚本
#!/bin/bash
ALIYUN_REGISTRY="镜像仓库地址"
ALIYUN_NAME_SPACE="命名空间"
ALIYUN_REGISTRY_USER="用户名"
ALIYUN_REGISTRY_PASSWORD="密码"
IMAGES_FILE_PATH="./images.txt"
READY_IMAGE_FILE="./ready_image.txt"
TEMP_IMAGE_FILE="./temp_images.txt"
echo "清理临时文件..."
rm -f "$TEMP_IMAGE_FILE"
echo "重启Docker服务..."
sudo service docker restart
echo "设置Docker Buildx..."
docker buildx create --use
echo "登录阿里云Docker镜像仓库..."
docker login -u "$ALIYUN_REGISTRY_USER" -p "$ALIYUN_REGISTRY_PASSWORD" "$ALIYUN_REGISTRY" || {
echo "登录失败,请检查用户名和密码。"
exit 1
}
touch "$READY_IMAGE_FILE"
echo "过滤需要处理的镜像..."
grep -Fvx -f "$READY_IMAGE_FILE" "$IMAGES_FILE_PATH" | grep -Ev '^\s*$|^\s*#' > "$TEMP_IMAGE_FILE"
if [ ! -s "$TEMP_IMAGE_FILE" ]; then
echo "没有需要处理的镜像。"
exit 0
fi
total=$(grep -c . "$TEMP_IMAGE_FILE")
current=0
echo "开始处理未处理的镜像,共计 $total 个镜像..."
while IFS= read -r image_name || [ -n "$image_name" ]; do
current=$((current + 1))
echo "处理镜像 $current/$total: $image_name"
echo "拉取镜像: $image_name"
docker pull "$image_name"
echo "镜像已拉取: $image_name"
platform=$(echo "$image_name" | awk -F'--platform[ =]' '{print $2}' | awk '{print $1}')
if [[ -n "$platform" && "$platform" == *"/"* ]]; then
platform_prefix="${platform//\//_}_"
else
platform_prefix=""
fi
image=$(echo "$image_name" | awk '{print $NF}')
image_name_tag=$(echo "$image" | awk -F'/' '{print $NF}' | awk -F'@' '{print $1}')
image_name=$(echo "$image_name_tag" | awk -F':' '{print $1}')
name_space=$(echo "$image" | awk -F'/' '{print $(NF-1)}')
name_space_prefix=""
if [[ -n "${duplicate_images[$image_name]}" && -n "$name_space" ]]; then
name_space_prefix="${name_space}_"
fi
new_image="$ALIYUN_REGISTRY/$ALIYUN_NAME_SPACE/${platform_prefix}${name_space_prefix}${image_name_tag}"
echo "镜像重命名:$new_image"
echo "推送镜像: $new_image"
docker tag "$image" "$new_image"
docker push "$new_image"
echo "镜像已推送: $new_image"
echo "清理镜像: $new_image"
docker rmi "$new_image"
echo "镜像已清理: $new_image"
echo "$image_name" >> "$READY_IMAGE_FILE"
echo "完成镜像处理 $current/$total: $image_name"
echo "----------------------------------------"
done < "$TEMP_IMAGE_FILE"
rm -f "$TEMP_IMAGE_FILE"
echo "清理原始镜像"
docker image prune -a -f
echo "所有镜像处理完成,共计 $total 个镜像。"
镜像列表示例
# MySQL
mysql
mysql:5.7
mysql:8