Docker-registry+Docker-registry UI 自动拉取镜像到Docker-registry

💥 原因:

  • 最近docker 镜像貌似都拉取不了了,用其他镜像源又网速慢。
  • 所有自己搭建私有docker镜像源,然后自己拉取,自己更新

✅ 搭建私用dockers镜像,并自动更新最新镜像到Docker-registry。

✅ 自动更新最新镜像到Docker-registry脚本。

✅ 使用方法:

  • 在脚本跟目录创建image_list.txt,并把镜像名称写入
  • 更新镜像脚本填入REGISTRY_URL,USERNAME,PASSWORD
  • 执行更新docker 最新镜像,并同步到私用镜像服务(Docker-registry)
#!/bin/bash

# 设置变量
REGISTRY_URL="docker.xxxx.cn"
IMAGE_LIST="image_list.txt"
# 替换为私有镜像仓库用户名
USERNAME="替换为私有镜像仓库用户名"  
# 替换为私有镜像仓库密码或访问令牌
PASSWORD="替换为私有镜像仓库密码或访问令牌"  

# 检查镜像列表文件是否存在
if [ ! -f "$IMAGE_LIST" ]; then
    echo e "\e[1;31m[ERROR]\e[0m 镜像列表文件不存在:$IMAGE_LIST"
    exit 1
fi


# 函数:获取镜像版本号
get_image_version() {
    local image="$1"
    # 使用docker inspect获取镜像版本号
    docker image inspect --format='{{index .RepoDigests 0}}' "$image" | cut -d '@' -f 2
}

# 创建本地镜像的镜像标签
while IFS= read -r image; do
    # 检查是否为空行或注释行
    if [[ -z "$image" || "$image" =~ ^# ]]; then
        continue
    fi

    # 精确查询本地是否存在该镜像并且版本是最新的
    if docker images --format '{{.Repository}}:{{.Tag}}' | grep -q "^$image\$"; then
        echo -e "\e[1;33m[WARNING]\e[0m 本地已存在最新版本的镜像:$image"
    else
        # 拉取最新版本的镜像
        echo -e "\e[1;32m[INFO]\e[0m 拉取最新版本的镜像:$image"
        docker pull "$image"
    fi

done < "$IMAGE_LIST"

# 登录私有镜像仓库
echo "登录私有镜像仓库:$REGISTRY_URL"
docker login -u "$USERNAME" -p "$PASSWORD" "$REGISTRY_URL"

# 推送镜像到私有镜像仓库
while IFS= read -r image; do
    # 检查是否为空行或注释行
    if [[ -z "$image" || "$image" =~ ^# ]]; then
        continue
    fi

    private_image="$REGISTRY_URL/$image"
    
    # 创建镜像标签并输出
    docker tag "$image" "$private_image"
    echo -e "\e[1;32m[INFO]\e[0m 创建镜像标签:$private_image"

    # 推送镜像到私有镜像仓库
    echo -e "\e[1;32m[INFO]\e[0m 推送镜像到私有镜像仓库:$private_image"
    docker push "$private_image"

    # 检查推送结果
    if [ $? -eq 0 ]; then
        echo -e "\e[1;32m[INFO]\e[0m 镜像推送成功:$private_image"
    else
        echo -e "\e[1;31m[ERROR]\e[0m 镜像推送失败:$private_image"
        continue
    fi

    # 删除本地镜像标签
    docker rmi "$private_image"
done < "$IMAGE_LIST"

# 脚本执行完毕,删除本地创建的镜像标签
echo -e "\e[1;31m[警告]\e[0m 脚本执行完毕,删除本地创建的镜像标签。"
docker logout "$REGISTRY_URL"

Saiyintai

软件测试工程师

相关推荐

Docker 设置网络代理

Docker Compose因网络超时更新失败,错误为请求Docker仓库时连接被取消。解决方法是在`/etc/docker/daemon.json`中配置HTTP和HTTPS代理为`http://127.0.0.1:7897`,并重启Docker服务。

停止并删除dockers 网络

Ubuntu 24.04部署ai-manus存在安全风险,因无用户登录功能。关闭容器时网络被占用,需先停止相关容器再执行docker compose down。项目功能达manus的80%,但消耗tokens过多。

为知笔记 docker 更新

停止并删除旧版为知笔记容器,拉取最新镜像后重新创建容器,映射存储目录和端口8081、9269。

暂无评论