zoukankan      html  css  js  c++  java
  • 命令行获取docker远程仓库镜像列表

    命令行获取docker远程仓库镜像列表

    获取思路

    通过curl获取镜像tagjson串,解析后得到${image}:${tag}的格式

    curl获取示例

    # curl [:-s] ${API}/${image}/tags
    curl https://registry.hub.docker.com/v1/repositories/nginx/tags
    

    docker-search.sh

    #!/bin/bash
    API="https://registry.hub.docker.com/v1/repositories"
    DEFAULT_NAME="nginx"
    DEFAULT_TIMEOUT=3
    
    function Usage(){
    cat << HELP
    
    Usage: docker-search NAME[:TAG]
    
    docker-search list all tags for docker image on a remote registry.
    
    Example:
        docker-search (default nginx)
        docker-search nginx
        docker-search nginx:1.15.8
        docker search nginx | docker-search
        docker search nginx | docker-search :1.15.8
        echo nginx | docker-search
        echo nginx | docker-search :1.15.8
    HELP
    }
    
    ARG=$1
    if [[ "$ARG" =~ "-h" ]];then
        Usage
        exit 0
    fi
    
    function ParseJson(){
        tr -d '[[]" ]' | tr '}' '
    ' | awk -F: -v image=$1 '{if(NR!=NF && $3 != ""){printf("%s:%s
    ",image,$3)}}'
    }
    
    function GetTags(){
        image=$1
        tag=$2
        ret=`curl -s ${API}/${image}/tags`
        tag_list=`echo $ret | ParseJson ${image}`
        if [ -z "$tag" ];then
            echo -e "$tag_list"
        else
            echo -e "$tag_list" | grep -w "$tag"
        fi
    }
    
    if [ -z $ARG ] || [[ ${ARG:0:1} == ":" ]];then
        if [ -x /usr/bin/timeout ];then
            images=`timeout $DEFAULT_TIMEOUT` awk '{print $1}' | grep -v "NAME" || echo $DEFAULT_NAME
        else
            images=`awk '{print $1}' | grep -v "NAME"`
        fi
    else
        images=`echo $ARG | awk -F: '{print $1}'`
    fi
    tag=`echo $ARG | awk -F: '{print $2}'`
    
    for i in ${images}
    do
        tags=`GetTags $i $tag`
        count=`echo $tags | wc -w`
        if [[ $count -gt 0 ]];then
            echo -e "IMAGE [$i:$tag]:"
            echo -e "$tags"
            echo
        fi
    done
    

    使用

    # 获取帮助
    $ docker-search --help
    
    # 查询ngnix
    $ docker-search nginx
    

    参考

    docker-tags 命令行获取docker远程仓库上指定镜像的tag列表

  • 相关阅读:
    Vue Router路由组件传参
    Object.defineProperty()详解
    响应状态码
    ngnix端口转发
    查看端口占用情况
    nginx的查看、启动、停止、重载命令
    nginx的几个默认路径
    pm2的一些常用命令
    为什么要学习HTML?HTML会过时吗?
    48.MySQL数据库使用(二)
  • 原文地址:https://www.cnblogs.com/xcmelody/p/11112205.html
Copyright © 2011-2022 走看看