命令行獲取docker遠程倉庫鏡像列表
獲取思路
經過curl
獲取鏡像tag
的json
串,解析後獲得${image}:${tag}
的格式nginx
curl獲取示例docker
# curl [:-s] ${API}/${image}/tags curl https://registry.hub.docker.com/v1/repositories/nginx/tags
獲取腳本docker-search
docker-search.shjson
#!/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 '}' '\n' | awk -F: -v image=$1 '{if(NR!=NF && $3 != ""){printf("%s:%s\n",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