我有以下代码,并且对API的调用返回如下错误,我还在下面粘贴了Docker Daemon命令。我尝试过使用/不使用TLS的HTTP / HTTPS / TCP的一些组合。
我在哪里可能出错?
“恐慌:尝试连接时发生错误:获取https://172.28.8.212:2375/v1.24/containers/json?limit=0:http:server向HTTPS客户端发出HTTP响应 “
func main() {
var headers map[string]string
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
cl := &http.Client{Timeout: time.Minute}
cli, err := client.NewClient("tcp://172.28.8.212:2375", "1.24", cl, headers)
if err != nil {
panic(err)
}
containers, err := cli.ContainerList(context.Background(), types.ContainerListOptions{})
if err != nil {
panic(err)
}
for _, container := range containers {
fmt.Printf("%s %s\n", container.ID[:10], container.Image)
}
}
我的Docker守护程序启动如下
[Unit]
Description = Docker Service Daemon
[Service]
ExecStart=/usr/bin/docker daemon -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock --cluster-store=consul://172.28.8.211:8500
答案 0 :(得分:-1)
func main() {
var headers map[string]string
tr := &http.Transport{} <---- Note the difference here.
tr.Dial = func(proto, addr string) (net.Conn, error) {
fmt.Println("Dial called")
conn, err := net.DialTimeout(proto, addr, time.Minute)
if err != nil {
fmt.Println("There was an err", err)
}
return conn, err
}
cl := &http.Client{Transport: tr}
cli, err := client.NewClient("http://x.y.z.w:2376", "1.24", cl, headers)
if err != nil {
panic(err)
}
containers, err := cli.ContainerList(context.Background(), types.ContainerListOptions{})
if err != nil {
panic(err)
}
for _, container := range containers {
fmt.Printf("%s %s\n", container.ID[:10], container.Image)
}
}