命令行获取docker远程仓库镜像列表
获取思路
通过curl
获取镜像tag
的json
串,解析后得到${image}:${tag}
的格式
curl获取示例
# curl [:-s] ${API}/${image}/tagscurl https://registry./v1/repositories/nginx/tags
获取脚本docker-search
docker-search.sh
#!/bin/bashAPI="https://registry./v1/repositories"DEFAULT_NAME="nginx"DEFAULT_TIMEOUT=3function Usage(){cat << HELPUsage: docker-search NAME[:TAG]docker-search list all tags for docker image on a remote registry.Example:docker-search (default nginx)docker-search nginxdocker-search nginx:1.15.8docker search nginx | docker-searchdocker search nginx | docker-search :1.15.8echo nginx | docker-searchecho nginx | docker-search :1.15.8HELP}ARG=$1if [[ "$ARG" =~ "-h" ]];thenUsageexit 0fifunction ParseJson(){tr -d '[\[\]" ]' | tr '}' '\n' | awk -F: -v image=$1 '{if(NR!=NF && $3 != ""){printf("%s:%s\n",image,$3)}}'}function GetTags(){image=$1tag=$2ret=`curl -s ${API}/${image}/tags`tag_list=`echo $ret | ParseJson ${image}`if [ -z "$tag" ];thenecho -e "$tag_list"elseecho -e "$tag_list" | grep -w "$tag"fi}if [ -z $ARG ] || [[ ${ARG:0:1} == ":" ]];thenif [ -x /usr/bin/timeout ];thenimages=`timeout $DEFAULT_TIMEOUT` awk '{print $1}' | grep -v "NAME" || echo $DEFAULT_NAMEelseimages=`awk '{print $1}' | grep -v "NAME"`fielseimages=`echo $ARG | awk -F: '{print $1}'`fitag=`echo $ARG | awk -F: '{print $2}'`for i in ${images}dotags=`GetTags $i $tag`count=`echo $tags | wc -w`if [[ $count -gt 0 ]];thenecho -e "IMAGE [$i:$tag]:"echo -e "$tags"echofidone
使用
# 获取帮助$ docker-search --help# 查询ngnix$ docker-search nginx
参考
docker-tags 命令行获取docker远程仓库上指定镜像的tag列表