golang访问docker hub获取所有镜像信息
可以使用Docker Hub API来访问Docker Hub并获取所有镜像信息。
以下是一个示例代码,使用Go语言访问Docker Hub API并获取所有镜像信息:
package main
import (
"encoding/json"
"fmt"
"net/http"
)
func main() {
// 创建HTTP客户端
client := &http.Client{}
// 发送GET请求
req, err := http.NewRequest("GET", "https://hub.docker.com/v2/repositories/", nil)
if err != nil {
panic(err)
}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
// 解析JSON响应
var data map[string]interface{}
err = json.NewDecoder(resp.Body).Decode(&data)
if err != nil {
panic(err)
}
// 输出所有镜像信息
for _, repo := range data["results"].([]interface{}) {
fmt.Printf("%s\n", repo.(map[string]interface{})["name"])
}
}
注意,访问Docker Hub API需要进行身份验证。可以在Docker Hub网站上创建一个API令牌,并将其添加到HTTP请求的Authorization标头中。例如:
req.Header.Set("Authorization", "Bearer <your-api-token>")
``
原文地址: https://www.cveoy.top/t/topic/fhZ2 著作权归作者所有。请勿转载和采集!