使用方法:

  • 复制代码保存为 image_registry.sh
  • sh image_registry.sh  -h   #查看帮助
  • HUB=10.0.29.104:5000 改为自己的地址
#!/bin/bash
#cnetos7,docker-ce v18.09.9,registry v2.6.2
#Docker registry 私有仓库镜像查询、删除、上传、下载 #Author caoweixiong <249299170@qq.com>
#Blog https://www.cnblogs.com/caoweixiong #root
[[ $UID -ne 0 ]] && { echo "Run in root user !";exit; }
#need jq ,get json data
[[ -f /usr/bin/jq ]] || { echo 'install jq';yum install -y jq &>/dev/null; } #参数 variable
#registry容器名称,默认registry
RN=${RN:-registry}
#访问网址,默认localhost:5000
HUB=${HUB:-localhost:5000}
HUB=10.0.29.104:5000 # 检测 check
function Check_hub() {
[[ `curl -s $HUB/v2/_catalog` == "Failed connect" ]] && { echo -e "\033[31m$HUB 访问失败\033[0m";exit; }
} # 删除images
function Delete_img() {
for n in $IMGS;
do
IMG=${n%%:*}
TAG=${n##*:}
echo "IMG=${IMG}"
echo "TAG=${TAG}"
i=1
[[ "$IMG" == "$TAG" ]] && { TAG=latest; n="$n:latest"; }
Digest=`curl --header "Accept: application/vnd.docker.distribution.manifest.v2+json" -Is ${HUB}/v2/${IMG}/manifests/${TAG} |awk '/Digest/ {print $NF}'`
echo "Digest=${Digest}"
[[ -z "$Digest" ]] && { echo -e "\033[31m$IMG:$TAG 镜像不存在\033[0m";} || {
URL="${HUB}/v2/${IMG}/manifests/${Digest}"
echo "URL=${URL}"
Rs=$(curl -Is -X DELETE ${URL%?}|awk '/HTTP/ {print $2}')
echo "Rs=${Rs}"
[[ $Rs -eq 202 ]] && { let i++;echo "$n 删除成功"; } || { echo -e "\033[31m$n 删除失败\033[0m"; }
}
done
#registry垃圾回收 RN=registry
[[ "$i" -gt 1 ]] && {
echo "Clean...";
docker exec ${RN} /bin/registry garbage-collect /etc/docker/registry/config.yml &>/dev/null;
docker restart ${RN} &>/dev/null;
}
} # 删除镜像所在目录(清除所有 -dd .* )
# 简单高效,删库跑路,必备技能
function Delete_img_a() {
[[ -f /usr/bin/docker ]] || echo 'No docker !'
[[ -z $(docker ps |awk '/'$RN'/ {print $NF}') ]] && { echo "$RN容器不存在!";exit; }
for n in $IMGS;
do
IMG="${n%%:*}"
docker exec $RN rm -rf /var/lib/registry/docker/registry/v2/repositories/$IMG
echo "$IMG 删除成功"
done
echo '清理 Clean ...'
docker exec $RN bin/registry garbage-collect /etc/docker/registry/config.yml &>/dev/null
docker restart $RN &>/dev/null
} # 上传 push
function Push() {
for IMG in $IMGS;
do
echo -e "\033[33m docker push $IMG to $HUB \033[0m"
docker tag $IMG $HUB/$IMG
docker push $HUB/$IMG
docker rmi $HUB/$IMG &>/dev/null
done
} # 下载 pull
function Pull() {
for IMG in $IMGS;
do
echo -e "\033[33m dokcer pull $IMG from $HUB \033[0m"
docker pull $HUB/$IMG
docker tag $HUB/$IMG $IMG
docker rmi $HUB/$IMG &>/dev/null
done
} # 查询images
function Select_img() {
IMG=$(curl -s $HUB/v2/_catalog |jq .repositories |awk -F'"' '{for(i=1;i<=NF;i+=2)$i=""}{print $0}')
[[ $IMG = "" ]] && { echo -e "\033[31m$HUB 没有docker镜像\033[0m";exit; }
#echo "$HUB Docker镜像:"
for n in $IMG;
do
echo "*****************【$n】******************"
TAG=$(curl -s http://$HUB/v2/$n/tags/list |jq .tags |awk -F'"' '{for(i=1;i<=NF;i+=2)$i=""}{print $0}')
for t in $TAG;
do
echo "$n:$t";
done
done
} case "$1" in
"-h")
echo
echo "#默认查询images"
echo "sh $0 -h #帮助 -d #删除 -dd #清理空间"
echo " -pull img1 img2 #下载 -push #上传"
echo
echo "#示例:删除 nginx:1.1 nginx:1.2 (镜像名:版本)"
echo "sh $0 -d nginx:1.1 nginx:1.2 "
echo "sh $0 -dd nginx #删除nginx所有版本"
echo
echo "#定义仓库url地址hub.test.com:5000(默认 localhost:5000)"
echo "env HUB=hub.test.com:5000 /bin/sh $0 -d nginx:1.1 "
echo
;;
"-d")
Check_hub
IMGS=${*/-dd/}
echo "IMGS=${IMGS}"
IMGS=${IMGS/-d/}
echo "IMGS=${IMGS}"
Delete_img
;;
"-dd")
Check_hub
IMGS=${*/-dd/}
IMGS=${IMGS/-d/}
Delete_img_a
;;
"-pull")
IMGS=${*/-pull/}
Pull
;;
"-push")
IMGS=${*/-push/}
Push
;;
*)
Check_hub
Select_img
;;
esac

引用:https://www.cnblogs.com/elvi/p/8384675.html

Docker——Registry 通过Shell管理私有仓库镜像的更多相关文章

  1. Docker registry 私有仓库镜像查询、删除、上传、下载 shell

    #Docker官方私有仓库registry #官方只提供了API接口,不方便使用,就写了个shell #docker-registry安装配置http://www.cnblogs.com/elvi/p ...

  2. docker之私有仓库镜像管理

    一.查看本地镜像 二.给镜像打标记(tag ) [root@node03 ~]# docker tag wordpress:v1 192.168.1.197:5000/wordpress:v1 2.删 ...

  3. Python Docker 查看私有仓库镜像【转】

    文章来源:python Docker 查看私有仓库镜像 pip 安装: # 首先安装epel扩展源: yum -y install epel-release # 更新完成之后,就可安装pip: yum ...

  4. Docker容器之搭建本地私有仓库

    Docker容器之搭建本地私有仓库 本地私有仓库搭建的具体步骤 首先下载 registry 镜像 docker pull registry 在 daemon.json 文件中添加私有镜像仓库的地址并重 ...

  5. Docker以http访问Harbor私有仓库(一)

    1 说明 前文Centos7搭建Harbor私有仓库(一)我们成功搭建Harbor,本篇我们主要配置Docker以http方式访问私有仓库 2 Docker配置 2.1 Mac系统 2.1.1 配置D ...

  6. docker 私有仓库镜像的存储位置

    docker 私有仓库的镜像 是存储在5739360d1030 registry "docker-registry" 3 days ago Up 28 hours 0.0.0.0: ...

  7. Docker删除私有仓库镜像

    V2 安装删除脚本 # curl https://raw.githubusercontent.com/burnettk/delete-docker-registry-image/master/dele ...

  8. kubernetes入门之获取私有仓库镜像

    一般情况下,我们项目构建的镜像统一会推送至私有仓库,那么这里大家可以参考阿里云的私有仓库搭建教程.那么我们可以通过以下步骤拉取: 1.推送及拉取镜像 1.1. 登录阿里云Docker Registry ...

  9. k8s之使用secret获取私有仓库镜像

    一.前言 其实这次实践算不上特别复杂,只是在实践过程中遇到了一些坑,以及填坑的方法是非常值得在以后的学习过程中参考借鉴的 二.知识准备 1.harbor是一个企业级的镜像仓库,它比起docker re ...

随机推荐

  1. java面试-JVM常用的基本配置参数有哪些?

    1.-Xms 初始大小内存,默认为物理内存 1/64,等价于 -XX:InitialHeapSize 2.-Xmx 最大分配内存,默认为物理内存的 1/4,等价于 -XX:MaxHeapSize 3. ...

  2. windows2003配置IIS

    这里采用的是vmbox虚拟机 用这个加载光驱, 使其加载Windows2003的安装镜像 挂载后根据下列操作 点击下一步等加载,加载到这个界面即可 在这里选择应用程序服务器(iis.asp.net), ...

  3. lvs 负载均衡 _DR模式 _Python脚本

    import paramiko vip='192.168.83.6' # 虚拟IP # direct_server_information ds_info={ 'ip':'192.168.83.5', ...

  4. Go-25-文件管理

    FileInfo接口 package main import ( "fmt" "os" ) // FileInfo 接口文件的信息包括文件名.文件大小.修改权限 ...

  5. Team Queue UVA - 540

      Queues and Priority Queues are data structures which are known to most computer scientists. The Te ...

  6. python 迭代器,生成器,表达式

    1.迭代器 (1)什么是迭代器: #迭代器即迭代的工具,那什么是迭代呢?#迭代是一个重复的过程,每次重复即一次迭代,并且每次迭代的结果都是下一次迭代的初始值 while True: #只是单纯地重复, ...

  7. map的遍历方法 map.entrySet()

    代码: @Test public void TestMap() throws Exception { Map<String, String> map = new HashMap<&g ...

  8. new word

    strategy: a plan of action or policy designed to achieve a major or overall aim.

  9. MyBatisPlus入门学习

    目录 MyBatisPlus 概述 快速入门 配置日志输出 CRUD拓展 插入 主键生成策略 更新操作 自动填充 乐观锁 查询操作 分页查询 删除操作 逻辑删除 性能分析插件 条件构造器 代码自动生成 ...

  10. 5-tcp套接字服务端编程

    import socket 1.创建套接字 sockfd= socket.socket(socket_family = AF_INIT,socket_type=SOCK_STREAM,proto) 功 ...