英文:
Docker API call returns "server gave HTTP response to HTTPS client"
问题
我有以下代码,并且调用API返回了以下错误。我也粘贴了Docker Daemon命令。我尝试了一些HTTP/HTTPS/TCP的组合,包括使用/不使用TLS。
我可能在哪里出错了?
"panic: An error occurred trying to connect: Get https://172.28.8.212:2375/v1.24/containers/json?limit=0: http: server gave HTTP response to HTTPS client"
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
英文:
I have the following Code and a Call to the API returns an error as follows, I hve also pasted Docker Daemon command below. I have tried a few combinations from HTTP/ HTTPS / TCP with / without TLS.
Where could I be wrong here?
"panic: An error occurred trying to connect: Get https://172.28.8.212:2375/v1.24/containers/json?limit=0: http: server gave HTTP response to HTTPS client
"
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)
}
}
My Docker Daemon is started as follows
[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
答案1
得分: -1
func main() {
var headers map[string]string
tr := &http.Transport{} // 注意这里的区别。
tr.Dial = func(proto, addr string) (net.Conn, error) {
fmt.Println("调用了Dial")
conn, err := net.DialTimeout(proto, addr, time.Minute)
if err != nil {
fmt.Println("发生了错误", 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)
}
}
英文:
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)
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论