Docker API调用返回”服务器向HTTPS客户端提供了HTTP响应”。

huangapple go评论102阅读模式
英文:

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"

  1. func main() {
  2. var headers map[string]string
  3. tr := &http.Transport{
  4. TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
  5. }
  6. cl := &http.Client{Timeout: time.Minute}
  7. cli, err := client.NewClient("tcp://172.28.8.212:2375", "1.24", cl, headers)
  8. if err != nil {
  9. panic(err)
  10. }
  11. containers, err := cli.ContainerList(context.Background(), types.ContainerListOptions{})
  12. if err != nil {
  13. panic(err)
  14. }
  15. for _, container := range containers {
  16. fmt.Printf("%s %s\n", container.ID[:10], container.Image)
  17. }
  18. }

我的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
"

  1. func main() {
  2. var headers map[string]string
  3. tr := &http.Transport{
  4. TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
  5. }
  6. cl := &http.Client{Timeout: time.Minute}
  7. cli, err := client.NewClient("tcp://172.28.8.212:2375", "1.24", cl, headers)
  8. if err != nil {
  9. panic(err)
  10. }
  11. containers, err := cli.ContainerList(context.Background(), types.ContainerListOptions{})
  12. if err != nil {
  13. panic(err)
  14. }
  15. for _, container := range containers {
  16. fmt.Printf("%s %s\n", container.ID[:10], container.Image)
  17. }
  18. }

My Docker Daemon is started as follows

  1. [Unit]
  2. Description = Docker Service Daemon
  3. [Service]
  4. 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

  1. tr := &http.Transport{} // 注意这里的区别。
  2. tr.Dial = func(proto, addr string) (net.Conn, error) {
  3. fmt.Println("调用了Dial")
  4. conn, err := net.DialTimeout(proto, addr, time.Minute)
  5. if err != nil {
  6. fmt.Println("发生了错误", err)
  7. }
  8. return conn, err
  9. }
  10. cl := &http.Client{Transport: tr}
  11. cli, err := client.NewClient("http://x.y.z.w:2376", "1.24", cl, headers)
  12. if err != nil {
  13. panic(err)
  14. }
  15. containers, err := cli.ContainerList(context.Background(), types.ContainerListOptions{})
  16. if err != nil {
  17. panic(err)
  18. }
  19. for _, container := range containers {
  20. fmt.Printf("%s %s\n", container.ID[:10], container.Image)
  21. }

}

英文:
  1. func main() {
  2. var headers map[string]string
  3. tr := &http.Transport{} <---- Note the difference here.
  4. tr.Dial = func(proto, addr string) (net.Conn, error) {
  5. fmt.Println("Dial called")
  6. conn, err := net.DialTimeout(proto, addr, time.Minute)
  7. if err != nil {
  8. fmt.Println("There was an err", err)
  9. }
  10. return conn, err
  11. }
  12. cl := &http.Client{Transport: tr}
  13. cli, err := client.NewClient("http://x.y.z.w:2376", "1.24", cl, headers)
  14. if err != nil {
  15. panic(err)
  16. }
  17. containers, err := cli.ContainerList(context.Background(), types.ContainerListOptions{})
  18. if err != nil {
  19. panic(err)
  20. }
  21. for _, container := range containers {
  22. fmt.Printf("%s %s\n", container.ID[:10], container.Image)
  23. }
  24. }

huangapple
  • 本文由 发表于 2016年11月15日 06:21:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/40598804.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定