将Docker图像标记解析为组件
规范化Docker图像标记的形式如下:将Docker图像标记解析为组件,docker,docker-registry,Docker,Docker Registry,规范化Docker图像标记的形式如下: [[registry-address]:port/]name:tag 地址和端口可以省略,在这种情况下,Docker转到默认注册表,即Docker集线器。例如,以下各项均有效: ubuntu:latest nixos/nix:1.10 localhost:5000/myfirstimage:latest localhost:5000/nixos/nix:latest 我需要一些代码将这个字符串可靠地解析为它的组成部分。但是,似乎不可能明确地做到这一点,
[[registry-address]:port/]name:tag
地址和端口可以省略,在这种情况下,Docker转到默认注册表,即Docker集线器。例如,以下各项均有效:
ubuntu:latest
nixos/nix:1.10
localhost:5000/myfirstimage:latest
localhost:5000/nixos/nix:latest
我需要一些代码将这个字符串可靠地解析为它的组成部分。但是,似乎不可能明确地做到这一点,因为“name”组件可以包含斜杠。例如,以下标记不明确:
localhost/myfirstimage:latest
这可能是Docker Hub上名为localhost/myfirstimage
的映像,也可能是在地址localhost
处运行的注册表上名为myfirstimage
的映像
有人知道Docker自己是如何解析这些输入的吗?我相信它确实可以被毫不含糊地解析 据 当前用于标识图像的语法如下所示: [registry_hostname[:port]/][user_name/](存储库_name[:version_tag]|图像_id) localhost是唯一允许的单一名称主机。所有其他端口必须包含一个端口(“:”)或多个部分(“foo.bar”,因此包含“.”) 实际上,这意味着,如果docker映像标识符以localhost开头,那么它将根据在localhost:80上运行的注册表进行解析
>docker拉本地主机/myfirstimage:最新
拉取存储库localhost/myfirstimage
拉取图像时出错:获取http://localhost/v1/repositories/myfirstimage/images: 拨打tcp 127.0.0.1:80:getsockopt:连接被拒绝
(使用Docker 1.12.0进行测试)
“也一样。”
>docker拉a.myfirstimage/名称:最新
使用默认标记:最新
来自守护进程的错误响应:Gethttps://a.myfirstimage/v1/_ping: 拨打tcp:在127.0.0.1:53上查找a.myfirstimage:没有这样的主机
及“:”
>docker pull myfirstimage:80/名称:最新
来自守护进程的错误响应:Gethttps://myfirstimage:80/v1/_ping: 在127.0.0.1:53上拨打tcp:lookup myfirstimage:没有这样的主机
因此,您的解析代码应该查看第一个“/”之前的子字符串,并检查它是“localhost”,还是包含“.”或以“:XYZ”(端口号)结尾,在这种情况下,它是注册表\主机名,否则它是存储库名称(用户名/存储库\名称)
实现此功能的Docker代码似乎位于以下位置:
及
//SplitReposeArchTerm将搜索项分解为索引名称和远程名称
func SplitReposeArchTerm(reposName字符串)(字符串,字符串){
nameParts:=strings.SplitN(reposName,“/”,2)
变量indexName,remoteName字符串
如果len(nameParts)==1 | |(!strings.Contains(nameParts[0],“)&&
!strings.Contains(nameParts[0],“:”&&nameParts[0]!=“localhost”){
//这是Docker索引回购(例如:samalba/hipache或ubuntu)
//“docker.io”
indexName=indexName
remoteName=reposName
}否则{
indexName=nameParts[0]
remoteName=nameParts[1]
}
返回indexName,remoteName
}
(虽然我没有详细调查过)可能有用。