douzai3399 2017-03-15 16:57
浏览 135
已采纳

使用Golang API的``docker run''(Docker文档)

I am trying to use Docker's tutorial in recreating a docker run. Here is the following code from online tutorial

 package main

 import (
 "io"
 "os"

"github.com/docker/docker/client"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"golang.org/x/net/context"
 )

 func main() {
ctx := context.Background()
cli, err := client.NewEnvClient()
if err != nil {
    panic(err)
}

_, err = cli.ImagePull(ctx, "alpine", types.ImagePullOptions{})
if err != nil {
    panic(err)
}

resp, err := cli.ContainerCreate(ctx, &container.Config{
    Image: "alpine",
    Cmd:   []string{"echo", "hello world"},
}, nil, nil, "")
if err != nil {
    panic(err)
}

if err := cli.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{});     err != nil {
    panic(err)
}

if _, err = cli.ContainerWait(ctx, resp.ID); err != nil {
    panic(err)
}

out, err := cli.ContainerLogs(ctx, resp.ID, types.ContainerLogsOptions{ShowStdout: true})
if err != nil {
    panic(err)
}

io.Copy(os.Stdout, out)
}

The problem I see with this is, if 'alpine' docker, is not available locally, it doesn't pull the latest and ends up throwing an error. e.g XXXXX$ go run go_docker.go panic: Error: No such image: alpine

    goroutine 1 [running]:
      panic(0x27ffa0, 0xc4202afa50)
    /usr/local/go/src/runtime/panic.go:500 +0x1a1
    main.main()
    /Users/rvenkatesh/go_coding/raghu_test_code/go_docker.go:30 +0x592
    exit status 2

But when I run the commandline equivalent, I see

    XXXX$ docker run alpine echo hello world
    Unable to find image 'alpine:latest' locally
    latest: Pulling from library/alpine 
    627beaf3eaaf: Pull complete

        Digest:sha256:58e1a1bb75db1b5a24a462dd5e2915277ea06438c3f105138f97eb53149673c4
    Status: Downloaded newer image for alpine:latest
    hello world

I tried looking through Go client, do I need to tweak anything with ImagePull function? Any help here would be appreciated!

Here is the link to the docs https://docs.docker.com/engine/api/getting-started/

Update: I had tested the same tutorial for python version, and it worked just fine. I wonder if the Golang page needs update.

  • 写回答

3条回答 默认 最新

  • dtxpz8785 2017-06-06 15:28
    关注

    Was having the same issue, the "Pull" didn't seem to be working. Found a Fix though.

    1) Modify your pull line to

    pullstat, err = cli.ImagePull(ctx, "alpine", types.ImagePullOptions{})
    

    and add

    io.Copy(os.StdOut,pullstat) 
    

    after the ImagePull

    I haven't tried doing an

    io.Copy(nil,pullstat) 
    

    but that's on my list of things to try next.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办