doushi9376 2018-01-05 19:19
浏览 100

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?

  • 写回答

1条回答 默认 最新

  • duanmei1850 2018-01-05 22:21
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择