Golang HTTP POST成功,但它不会调用docker操作

时间:2022-09-25 14:43:37

Problem: Although I can easily issue GET and POST commands from curl on my local docker socket, when I try to do the POST equivalent in Golang for a docker pull action, using net.Dial, I see no action taken on Docker's behalf.

问题:虽然我可以在本地docker socket上轻松地从curl发出GET和POST命令,但是当我尝试在Golang中使用netdd执行POST等效操作时,使用net.Dial,我看不到代表Docker采取任何操作。

Note that, meanwhile, the GET action works just fine using docker sockets via the golang client.

请注意,同时,GET操作通过golang客户端使用docker套接字正常工作。

For example, when running the code at the bottom of this post, I see:

例如,当运行此帖子底部的代码时,我看到:

2018/01/05 14:16:33 Pulling http://localhost/v1.24/images/create?fromImage=fedora&tag=latest ......
2018/01/05 14:16:34 Succeeded pull for http://localhost/v1.24/images/create?fromImage=fedora&tag=latest &{200 OK 200 HTTP/1.1 1 1 map[Docker-Experimental:[true] Ostype:[linux] Server:[Docker/17.09.0-ce (linux)] Api-Version:[1.32] Content-Type:[application/json] Date:[Fri, 05 Jan 2018 19:16:34 GMT]] 0xc42010a100 -1 [chunked] false false map[] 0xc420102000 <nil>}

The code for attempting to do a pull via a POST action:

尝试通过POST操作执行拉取的代码:

// pullImage is the equivalent of curl --unix-socket /var/run/docker.sock -X POST http://localhost/images/create?fromImage=alpine
func pullImage(img string, tag string) (err error) {
        fd := func (proto, addr string) (conn net.Conn, err error) {
                return net.Dial("unix", "/var/run/docker.sock")
        }
        tr := &http.Transport{
                Dial: fd,
        }
        client := &http.Client{Transport: tr}
        imageUrl := fmt.Sprintf("http://localhost/v1.24/images/create?fromImage=%s&tag=%s", img, tag)
        log.Printf("Pulling %s ......  ", imageUrl)
        resp, err := client.Post(imageUrl, "application/json", nil)
        if resp.StatusCode == 200 && err == nil {
                log.Printf("Succeeded pull for %s %v",imageUrl, resp)
        } else {
                log.Printf("FAILED pull for %s , ERROR = ((  %s  )) ",imageUrl , err)
        }
        return err

Question:

What does curl --unix-socket /var/run/docker.sock -X POST http://localhost/v1.24/images/create?fromImage=fedora do differently then the Golang code in the above snippet?

什么curl --unix-socket /var/run/docker.sock -X POST http://localhost/v1.24/images/create?fromImage = fedora与上面代码段中的Golang代码有什么不同?

1 个解决方案

#1


0  

I figured this out. Actually, my prolem was that I wasn't completing the response body.

我想出来了。实际上,我的原因是我没有完成响应机构。

Adding

defer resp.Body.Close()

after the rest, err := client... clause was enough to finish triggering an entire pull. Not quite sure why closing the response body effects the ability of docker to start pulling the images down, but in any case, that was the case.

在休息之后,错误:= client ...子句足以完成触发整个拉动。不太清楚为什么关闭响应体会影响docker开始拉下图像的能力,但无论如何,情况都是如此。

#1


0  

I figured this out. Actually, my prolem was that I wasn't completing the response body.

我想出来了。实际上,我的原因是我没有完成响应机构。

Adding

defer resp.Body.Close()

after the rest, err := client... clause was enough to finish triggering an entire pull. Not quite sure why closing the response body effects the ability of docker to start pulling the images down, but in any case, that was the case.

在休息之后,错误:= client ...子句足以完成触发整个拉动。不太清楚为什么关闭响应体会影响docker开始拉下图像的能力,但无论如何,情况都是如此。