Golang HTTP POST成功,但未调用docker操作

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.

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

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:

// 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?

dpi96151
dpi96151 一个好的调试步骤是在代码和Docker之间粘贴一个日志记录代理(例如,具有适当命令行的socat)。您的golang代码中的请求看起来与curl中的请求相同吗?
2 年多之前 回复
dotdx80642
dotdx80642 并且始终在响应之前检查错误,否则您会感到恐慌。
2 年多之前 回复
dongshao2967
dongshao2967 什么是“我看不到代表Docker采取的任何行动”。意思?您期望看到什么?另外,您是否看过json响应?它可能包含有用的信息。
2 年多之前 回复

1个回答



我知道了。 其实,我的难题是我没有完成响应正文。</ p>

添加</ p>

 延迟res.Body.Close()\  n </ code> </ pre> 

休息之后,err:= client ... </ code>子句足以触发整个拉动。 不太清楚为什么关闭响应主体会影响docker开始拉下图像的能力,但无论如何,就是这种情况。</ p>
</ div>

展开原文

原文

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.

douweidao3882
douweidao3882 它可能正在等待编写主体,但您从未读过它,并且关闭连接强制完成请求。 如果要完成请求并能够重用连接,请务必阅读响应正文。
2 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问