gpt4 book ai didi

go - Docker API 调用返回 "server gave HTTP response to HTTPS client"

转载 作者:数据小太阳 更新时间:2023-10-29 03:29:52 24 4
gpt4 key购买 nike

我有以下代码,调用 API 返回错误如下,我还在下面粘贴了 Docker Daemon 命令。我已经尝试了 HTTP/HTTPS/TCP 的一些组合,有/没有 TLS。

我哪里错了?

“ panic :尝试连接时发生错误:获取 https://172.28.8.212:2375/v1.24/containers/json?limit=0:http:服务器向 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 Daemon启动如下

[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

最佳答案

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)
}
}

关于go - Docker API 调用返回 "server gave HTTP response to HTTPS client",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40598804/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com