1、ssh连接docker,我的环境是群晖;
2、输入:vi pull_image.sh,粘贴如下代码
[Asm] 纯文本查看 复制代码#!/bin/bash -e
###############################################################################
# Copyright (c) 2024, 深圳市东微智能科技股份有限公司. All rights reserved.
#
# Author: weifangnan
# Date: 2024-11-29 08:06:45
# LastEditors: weifangnan
# LastEditTime: 2024-11-29 09:36:15
# Description: 国内拉取镜像加速
###############################################################################
# 检查是否传递了目标参数
if [ $# -ne 1 ]; then
echo "Usage: $0 "
exit
fi
IMG=$1
###############################################################################
# DockerHub 镜像源列表
DOCKERHUB_REGISTRY_MIRRORS=(
# "hub.xdark.top"
# "hub.littlediary.cn"
# "dockerpull.org"
# "hub.crdz.gq"
"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}。"
3,、按:健,输入wq,回车进行保存
4、输入:chmod +x pull_image.sh,给脚本加上可执行权限,即可成功拉取镜像
5、例如拉取ghcr.io/imagegenius/immich:latest; 输入:./pull_image.sh ghcr.io/imagegenius/immich:latest 就可以了。