我想使用v2 API在私有Docker注册表中查找图像的最新时间戳,而不先将图像拉到我的本地主机。
答案 0 :(得分:13)
因此,经过一些黑客攻击,我使用curl
和jq
工具完成了以下工作:
curl -X GET http://registry:5000/v2/<IMAGE>/manifests/<TAG> \
| jq -r '.history[].v1Compatibility' \
| jq '.created' \
| sort \
| tail -n1
这似乎有效,但我真的不知道如何解释v1兼容性表示,所以我不知道我是否真正得到了正确的数字。
欢迎对此发表评论!
答案 1 :(得分:1)
对@snth答案的一个小改进:使用date
打印日期对用户更友好:
date --date=$(curl -s -X GET http://$REGISTRY:5000/v2/$IMAGE/manifests/$TAG | \
jq -r '.history[].v1Compatibility' | jq -r '.created' | sort | tail -n 1 )
打印类似:
Fr 12. Okt 15:26:03 CEST 2018
答案 2 :(得分:1)
在此处整理以上评论和答案是一个完整的解决方案
# Setup variables to make this more usalbe
USERNAME=docker_user
PASSWORD=docker_pass
DOCKER_REGISTRY=http://my-registry.myorg.org:9080
REPO=myrepo
TAG=atag
# Query Registry and pipe results to jq
DOCKER_DATE=$(curl -s -u $USERNAME:$PASSWORD -H 'Accept: application/vnd.docker.distribution.manifest.v1+json' -X GET http://$REGISTRY_URL/v2/circle-lb/manifests/master | jq -r '[.history[]]|map(.v1Compatibility|fromjson|.created)|sort|reverse|.[0]')
echo "Date for $REPO:$TAG is $DOCKER_DATE"
DOCKER_DATE=$(curl -s -H 'Accept: application/vnd.docker.distribution.manifest.v1+json' -X GET http://$REGISTRY_URL/v2/circle-lb/manifests/master | jq -r '[.history[]]|map(.v1Compatibility|fromjson|.created)|sort|reverse|.[0]')
最后,如果要使用date命令(gnu date)进行解析
在Linux上:
date -d $DOCKER_DATE
在Mac上:
gdate -d $DOCKER_DATE
答案 3 :(得分:0)
这里是 dotnet core (C#) 实现,以防有人感兴趣:
public class DockerHub{
public DockerRegistryToken GetRegistryToken(string image){
Console.WriteLine("authenticateing with dockerhub");
using HttpClient client = new ();
var url = string.Format($"https://auth.docker.io/token?service=registry.docker.io&scope=repository:{image}:pull");
//var response = client.Send(new HttpRequestMessage(HttpMethod.Get, url));
var result = client.GetStringAsync(url);
var drt = JsonSerializer.Deserialize<DockerRegistryToken>(result.Result);
return drt;
}
public DateTime GetRemoteImageDate(string image, string tag, DockerRegistryToken token){
using HttpClient client = new ();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token.Token);
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/vnd.docker.distribution.manifest.list.v2+json"));
var manifestResult = client.GetStringAsync(string.Format($"https://registry-1.docker.io/v2/{image}/manifests/{tag}"));
var json = JsonDocument.Parse(manifestResult.Result);
var created = json.RootElement.GetProperty("history").EnumerateArray()
.Select(m => JsonDocument.Parse(m.GetProperty("v1Compatibility").ToString()).RootElement.GetProperty("created"))
.Select(m => DateTime.Parse(m.GetString()))
.OrderByDescending(m => m)
.FirstOrDefault(); // I challange you to improve this
Console.WriteLine("Date recieved: {0}",created);
return created.ToUniversalTime();
}
}
public class DockerRegistryToken{
[JsonPropertyName("token")]
public string Token { get; set; }
/// always null
[JsonPropertyName("access_token")]
public string AccessToken {get; set; }
[JsonPropertyName("expires_in")]
public int ExpiresInSeconds { get; set; }
[JsonPropertyName("issued_at")]
public DateTime IssuedAt { get; set; }
}
使用
var client = new DockerHub();
var tok = client.GetRegistryToken("your/reponame");
var remoteImageDate = client.GetRemoteImageDate("your/reponame","latest",tok);