博学笃行·盛德日新

Docker怎么查看远端仓库的标签


技术 docker

0x00 原因

当我们直接使用docker pull命令的时候,实际上都是pull对应镜像的latest标签或者默认标签。
比如我们 docker pull centos

# yongfu @ yongfus-MacBook-Pro in ~ [17:51:14]
$ docker pull centos
Using default tag: latest
latest: Pulling from library/centos
aeb7866da422: Pull complete
Digest: sha256:67dad89757a55bfdfabec8abd0e22f8c7c12a1856514726470228063ed86593b
Status: Downloaded newer image for centos:latest

# yongfu @ yongfus-MacBook-Pro in ~ [18:11:36]
$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              latest              75835a67d134        3 weeks ago         200MB

# yongfu @ yongfus-MacBook-Pro in ~ [18:11:39]
$ docker run -it 75835a67d134 /bin/bash
[root@efa402cdfddb /]# cat /etc/redhat-release
CentOS Linux release 7.5.1804 (Core)
[root@efa402cdfddb /]#

可以看到,实际上拉取的是最新的latest标签, 进入到系统里查看,是当前(20181106)最新的系统centos 7.5.1804
如果我们想要下载 centos 6 版本的系统呢,还需要去带上对应的标签进行下载。

0x01 获取远程的docker 远程的 tags

docker并没有提供一个直接的命令可以查询远程仓库的标签信息,但是提供的一个网页进行查询:

查询URL为:  https://registry.hub.docker.com/v1/repositories/【镜像名】/tags

比如 centos 的所有镜像标签查询url为:
https://registry.hub.docker.com/v1/repositories/centos/tags

0x02 写个脚本来获取远程的docker仓库标签tag

创建一个 ~/bin/dockertags.sh 的文件,文件内容如下:

#!/bin/bash

function usage() {
    cat << HELP

dockertags  --  list all tags for a Docker image on a remote registry.

EXAMPLE:
    - list all tags for ubuntu:
       dockertags ubuntu

    - list all php tags containing apache:
       dockertags php apache

HELP
}

if [ $# -lt 1 ]; then
    usage
    exit
fi

image="$1"
tags=`wget -q https://registry.hub.docker.com/v1/repositories/${image}/tags -O -  | sed -e 's/[][]//g' -e 's/"//g' -e 's/ //g' | tr '}' '\n'  | awk -F: '{print $3}'`

if [ -n "$2" ]
then
    tags=` echo "${tags}" | grep "$2" `
fi

echo "${tags}"

0x03 示例

给予权限:

chmod +x ~/bin/dockertags.sh

演示:

# yongfu @ yongfus-MacBook-Pro in ~ [18:28:21]
$ dockertags.sh

dockertags  --  list all tags for a Docker image on a remote registry.

EXAMPLE:
    - list all tags for ubuntu:
       dockertags ubuntu

    - list all php tags containing apache:
       dockertags php apache


# yongfu @ yongfus-MacBook-Pro in ~ [18:29:50]
$ dockertags.sh centos
latest
5
5.11
6
6.10
6.6
6.7
6.8
6.9
7
7.0.1406
7.1.1503
7.2.1511
7.3.1611
7.4.1708
7.5.1804
centos5
centos5.11
centos6
centos6.10
centos6.6
centos6.7
centos6.8
centos6.9
centos7
centos7.0.1406
centos7.1.1503
centos7.2.1511
centos7.3.1611
centos7.4.1708
centos7.5.1804

# yongfu @ yongfus-MacBook-Pro in ~ [18:30:01]
$ dockertags.sh centos centos6
centos6
centos6.10
centos6.6
centos6.7
centos6.8
centos6.9

0x04 参考

评论