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.

    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀