【问题标题】:Parsing Docker image tag into component parts [duplicate]将 Docker 映像标记解析为组件部分 [重复]
【发布时间】:2017-02-08 14:26:51
【问题描述】:

规范的 Docker 镜像标签格式为:

[[registry-address]:port/]name:tag

地址和端口可以省略,在这种情况下,Docker 会转到默认注册表,即 Docker Hub。例如以下都是有效的:

ubuntu:latest
nixos/nix:1.10
localhost:5000/myfirstimage:latest
localhost:5000/nixos/nix:latest

我需要一些代码来可靠地将这个字符串解析为它的组成部分。然而,似乎不可能明确地做到这一点,因为“名称”组件可以包含斜杠。例如下面的标签是不明确的:

localhost/myfirstimage:latest

这可能是 Docker Hub 上名为 localhost/myfirstimage 的映像,也可能是在地址 localhost 上运行的注册表上名为 myfirstimage 的映像。

有人知道 Docker 本身是如何解析此类输入的吗?

【问题讨论】:

标签: docker docker-registry


【解决方案1】:

我相信它确实可以被明确地解析。

根据this

当前用于识别图像的语法是这样的:

[registry_hostname[:port]/][user_name/](repository_name[:version_tag] | image_id)

...

localhost 是唯一允许的单名主机。所有其他必须包含一个端口(“:”)或多个部分(“foo.bar”,因此包含一个“.”)

实际上,这意味着如果您的 docker 镜像标识符以 localhost 开头,它将针对运行在 localhost:80

的注册表进行解析
>docker pull localhost/myfirstimage:latest
Pulling repository localhost/myfirstimage
Error while pulling image: Get http://localhost/v1/repositories/myfirstimage/images: dial tcp 127.0.0.1:80: getsockopt: connection refused

(使用 Docker 1.12.0 测试)

“也是一样。”

>docker pull a.myfirstimage/name:latest
Using default tag: latest
Error response from daemon: Get https://a.myfirstimage/v1/_ping: dial tcp: lookup a.myfirstimage on 127.0.0.1:53: no such host

":"

>docker pull myfirstimage:80/name:latest
Error response from daemon: Get https://myfirstimage:80/v1/_ping: dial tcp: lookup myfirstimage on 127.0.0.1:53: no such host

所以你的解析代码应该查看第一个"/"之前的子字符串,并检查它是"localhost",还是包含"。 " 或以 ":XYZ" 结尾(一个端口号),在这种情况下它是一个 registry_hostname,否则它是一个 repository name(用户名/repository_name)。

实现此功能的 Docker 代码似乎位于此处: reference.goservice.go

// splitReposSearchTerm breaks a search term into an index name and remote name
func splitReposSearchTerm(reposName string) (string, string) {
        nameParts := strings.SplitN(reposName, "/", 2)
        var indexName, remoteName string
        if len(nameParts) == 1 || (!strings.Contains(nameParts[0], ".") &&
                !strings.Contains(nameParts[0], ":") && nameParts[0] != "localhost") {
                // This is a Docker Index repos (ex: samalba/hipache or ubuntu)
                // 'docker.io'
                indexName = IndexName
                remoteName = reposName
        } else {
                indexName = nameParts[0]
                remoteName = nameParts[1]
        }
        return indexName, remoteName
}

(虽然我没有更详细地研究它)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-16
    • 1970-01-01
    • 1970-01-01
    • 2017-01-07
    • 2012-10-12
    • 1970-01-01
    • 2021-10-24
    • 1970-01-01
    相关资源
    最近更新 更多