LoginSignup
2
2

More than 1 year has passed since last update.

dockerイメージのタグ一覧を表示するコマンドを作る

Last updated at Posted at 2022-09-26

概要

dockerイメージのタグは、dockerhubから探すことができますが、
1ページに表示されるタグはそんなに多くなく、何回もページを進んで探すのはちょっと面倒かと思います。
以前はdockerhubのAPI
https://registry.hub.docker.com/v1/repositories/イメージ名/tags
を叩くと、タグ一覧を取得できて結構便利でした。
が、ちょっと前からできなくなったみたいです。
dockerhubにアクセスしてデベロッパーツールで調査してみたところ、今は
https://hub.docker.com/v2/repositories/library/イメージ名/tags
からいけるみたいです。
これを利用してdockerイメージのタグ一覧を取得するシェルスクリプトを作成してみました。

シェルスクリプトのコード

docker-tags
#!/bin/sh
# 2022/09/18

# 問い合わせ回数を取得
num=$(echo $@ | tr ' ' '\n' | grep '\-n' | tr -d '\-n')
# ページ番号を取得
page=$(echo $@ | tr ' ' '\n' | grep '\-page' | tr -d '\-page')

# 指定されたページ番号に問い合わせ
if [ -n "${page}" ]; then
  curl -s "https://hub.docker.com/v2/repositories/library/${1}/tags/?page_size=100&page=${page}" |
    sed -e 's/,/\n/g' |
    grep '\"name\":' |
    sed -e 's/:/ /g' -e 's/\"//g' | 
    awk '{print $2}'
  # 処理終了
  exit 0
fi

# 問い合わせ回数が指定されていなければデフォルトで1とする
if [ -z "${num}" ]; then
  num=1
fi

# 1ページ目から指定された回数のページまでを表示
seq ${num} | while read -r line; do
  curl -s "https://hub.docker.com/v2/repositories/library/${1}/tags/?page_size=100&page=${line}" |
    sed -e 's/,/\n/g' |
    grep '\"name\":' |
    sed -e 's/:/ /g' -e 's/\"//g' | 
    awk '{print $2}'
done

インストール方法

1)PATHが通っているディレクトリに配置するか、配置したディレクトリのPATHを通す

例)
・~/binというディレクトリを作成し、本スクリプトを配置
・~/binのPATHを通す

2)実行権限を与える

$ chmod +x docker-tags

使用方法

  • オプションなし
$ docker-tags イメージ名
# 1ページ分表示される
  • 表示ページ数を指定
$ docker-tags イメージ名 -n10
# 10ページ分表示される
  • 表示するページ番号を指定
$ docker-tags イメージ名 -page10
# 10ページ目のみ表示される

使用例

例として、"ubuntu"で検索してみます。

例)
$ docker-tags ubuntu

latest
bionic-20220902
bionic
18.04
rolling
kinetic-20220830
kinetic
jammy-20220815
jammy
focal-20220826
focal
devel
bionic-20220829
22.10
22.04
20.04
  ・
  ・
  ・

おまけ1:イメージ名の検索

イメージ名の検索は、dockerhubで行っても良いですが、コマンドでもできます。
docker searchを使います。

書式
$ docker search キーワード

例として、"ubuntu"で検索してみます。
スラッシュ"/"が付いている場合、スラッシュより後ろがイメージ名です。

例)
$ docker search ubuntu

ubuntu                           Ubuntu is a Debian-based Linux operating sys…   14955     [OK]
websphere-liberty                WebSphere Liberty multi-architecture images …   288       [OK]
ubuntu-upstart                   DEPRECATED, as is Upstart (find other proces…   112       [OK]
neurodebian                      NeuroDebian provides neuroscience research s…   93        [OK]
ubuntu/nginx                     Nginx, a high-performance reverse proxy & we…   59
open-liberty                     Open Liberty multi-architecture images based…   54        [OK]
ubuntu-debootstrap               DEPRECATED; use "ubuntu" instead                46        [OK]
ubuntu/apache2                   Apache, a secure & extensible open-source HT…   41
ubuntu/mysql                     MySQL open source fast, stable, multi-thread…   36
ubuntu/squid                     Squid is a caching proxy for the Web. Long-t…   32
kasmweb/ubuntu-bionic-desktop    Ubuntu productivity desktop for Kasm Workspa…   31
ubuntu/prometheus                Prometheus is a systems and service monitori…   29
ubuntu/bind9                     BIND 9 is a very flexible, full-featured DNS…   27
ubuntu/postgres                  PostgreSQL is an open source object-relation…   19
ubuntu/redis                     Redis, an open source key-value store. Long-…   11
ubuntu/kafka                     Apache Kafka, a distributed event streaming …   11
ubuntu/prometheus-alertmanager   Alertmanager handles client alerts from Prom…   7
ubuntu/grafana                   Grafana, a feature rich metrics dashboard & …   6
ubuntu/memcached                 Memcached, in-memory keyvalue store for smal…   5
ubuntu/zookeeper                 ZooKeeper maintains configuration informatio…   5
ubuntu/telegraf                  Telegraf collects, processes, aggregates & w…   4
ubuntu/dotnet-deps               Chiselled Ubuntu for self-contained .NET & A…   3
ubuntu/cortex                    Cortex provides storage for Prometheus. Long…   3
ubuntu/cassandra                 Cassandra, an open source NoSQL distributed …   2
ubuntu/loki                      Grafana Loki, a log aggregation system like …   0

おまけ2:対応しているアーキテクチャも一緒に表示する

特にM1Macなどを使用されている方は、対応しているアーキテクチャも気にしなければいけません。
タグ名:アーキテクチャ名という形式で出力できるよう、上記スクリプトを改変したのが以下のコードです。

※動けば良いという発想でしか書いてません。。。動けば良いんです動けば。。。
 jqコマンドとか使うともっとスマートに書けるのかな。。。

docker-tags
#!/bin/sh
# 2022/09/18

# 問い合わせ回数を取得
num=$(echo $@ | tr ' ' '\n' | grep '\-n' | tr -d '\-n')
# ページ番号を取得
page=$(echo $@ | tr ' ' '\n' | grep '\-page' | tr -d '\-page')

# 指定されたページ番号に問い合わせ
if [ -n "${page}" ]; then
  curl -s "https://hub.docker.com/v2/repositories/library/${1}/tags/?page_size=100&page=${page}" |
    sed -e 's/,/\n/g' |
    grep -E 'architecture|\"name\":' |
    tac |
    tr -d '\n' |
    sed -e 's/\"images\"//g' -e 's/\"name\":/\n/g' -e 's/{\"architecture\"//g' -e 's/\"//g' -e 's/\[://g' -e 's/:/ /g' |
    awk '{print $1":"$2,$3,$4,$5,$6,$7,$8,$9,$10}' |
    tac |
    sed -e 's/ *$//g' -e 's/ /, /g' -e 's/^:$//'
  # 処理終了
  exit 0
fi

# 問い合わせ回数が指定されていなければデフォルトで1とする
if [ -z "${num}" ]; then
  num=1
fi

# 1ページ目から指定された回数のページまでを表示
seq ${num} | while read -r line; do
  curl -s "https://hub.docker.com/v2/repositories/library/${1}/tags/?page_size=100&page=${line}" |
    sed -e 's/,/\n/g' |
    grep -E 'architecture|\"name\":' |
    tac |
    tr -d '\n' |
    sed -e 's/\"images\"//g' -e 's/\"name\":/\n/g' -e 's/{\"architecture\"//g' -e 's/\"//g' -e 's/\[://g' -e 's/:/ /g' |
    awk '{print $1":"$2,$3,$4,$5,$6,$7,$8,$9,$10}' |
    tac |
    sed -e 's/ *$//g' -e 's/ /, /g' -e 's/^:$//' |
    grep -vE '^$'
done

※tacを使用しています。ファイルの末尾から出力するコマンドで、catの逆だからtac。
 command not found: tacと表示される場合は、適宜インストールしてください。
 Macの場合は、brew install coreutilsです。

例として、"ubuntu"で検索してみます。

例)
$ docker-tags ubuntu

latest:s390x, riscv64, ppc64le, arm64, arm, amd64
bionic-20220902:s390x, ppc64le, 386, arm64, arm, amd64
bionic:s390x, ppc64le, 386, arm64, arm, amd64
18.04:s390x, ppc64le, 386, arm64, arm, amd64
rolling:s390x, riscv64, ppc64le, arm64, arm, amd64
kinetic-20220830:s390x, riscv64, ppc64le, arm64, arm, amd64
kinetic:s390x, riscv64, ppc64le, arm64, arm, amd64
jammy-20220815:s390x, riscv64, ppc64le, arm64, arm, amd64
jammy:s390x, riscv64, ppc64le, arm64, arm, amd64
focal-20220826:s390x, riscv64, ppc64le, arm64, arm, amd64
focal:s390x, riscv64, ppc64le, arm64, arm, amd64
devel:s390x, riscv64, ppc64le, arm64, arm, amd64
bionic-20220829:s390x, ppc64le, 386, arm64, arm, amd64
22.10:s390x, riscv64, ppc64le, arm64, arm, amd64
22.04:s390x, riscv64, ppc64le, arm64, arm, amd64
20.04:s390x, riscv64, ppc64le, arm64, arm, amd64
      ・
      ・
      ・
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2