【问题标题】:How to list all tags for every docker image present in catalog?如何列出目录中存在的每个 docker 图像的所有标签?
【发布时间】:2016-10-14 12:44:55
【问题描述】:

我正在使用以下命令列出我的私有注册表中的所有图像:

curl -s http://internal.private.registry.com/v2/_catalog | jq -r '.repositories[0:2] | to_entries | map( .value )[]'

输出:

centos
containersol/consul-server

我正在使用以下命令列出我的私人注册表中特定图像的所有标签:

 curl -s GET http://internal.private.registry.com/v2/centos/tags/list | jq -r '.tags | to_entries | map( .value )[]'

输出:

6.6
6
7.1.1503

现在,我正在尝试使用以下 bash 脚本列出目录中每个图像的所有标签:

#!/bin/sh

image_name=$(curl -s http://internal.private.registry.com/v2/_catalog | jq -r '.repositories[0:2] | to_entries | map( .value )[]')

while read -r line; do 
    ${line}$_image_taglist=$(curl -s GET http://internal.private.registry.com/v2/cybs/${line}/tags/list | jq -r '.tags | to_entries | map( .value )[]')
    while read -r tag; do
        echo "$tag"
     done <<< "${line}$_image_taglist"  
done <<< "$image_name"

但是,我在执行${line}$_image_taglist=$(curl -s GET http://internal.private.registry.com/v2/cybs/${line}/tags/list | jq -r '.tags | to_entries | map( .value )[]') 时遇到以下错误:

jq: error (at <stdin>:1): null (null) has no keys

另外,请注意,我希望 dynamic variable ${line}$_image_taglist 应该解析为 centos_image_taglistcontainersol/consul-server_image_taglist

【问题讨论】:

  • 您能提供输入 json 的样本吗?
  • 另外,你能指定两个 jq 调用中的哪一个产生了这个错误吗?您可以在脚本的开头(在 hashbang 下方)写“set -x”来查看正在打印的每一行。
  • @JeffMercado 我在原帖中提供了更多详细信息。
  • 不,不要向我们展示通过 jq 传递 json 的结果,向我们展示 json 本身,用于所有调用。
  • ${line}$_image_taglist=whatever 是语法错误。你到底想在这里完成什么?无论如何,您都在使用 Bash 构造(sh 通常不支持 &lt;&lt;&lt; here-string)所以我的猜测是您想使用 Bash 数组。但是内部的while read 看起来也很古怪。如果您想拆分一组字段,请尝试printf "%s\n" $string_with_many_whitespace_fields(但这是一个非常模糊的猜测;您的代码也没有真正做到这一点)。

标签: bash shell while-loop sh jq


【解决方案1】:

jq: error (at :1): null (null) has no keys

是由. 为空引起的。在这种情况下,. 指的是标准输入,这意味着您没有收到来自http://internal.private.registry.com/v2/cybs/${line}/tags/list 的响应。

您不能像这样定义动态命名变量:${line}$_image_taglist。如果你的 bash 版本支持,你应该使用关联数组:

declare -A arr
arr["$line"]=$(curl ... | jq ...)

但在你的情况下,我真的不明白你为什么需要它,因为你只是在迭代它。为什么不使用管道或进程替换?

while read -r tag; do
    echo "$tag"
 done < <(curl ... | jq ...)

【讨论】:

    猜你喜欢
    • 2015-04-03
    • 2020-09-19
    • 1970-01-01
    • 2011-11-09
    • 2021-11-16
    • 2021-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多