dongshu9458 2017-07-31 23:46
浏览 92
已采纳

为什么我无法为docker获取go sdk?

I am trying to run docker go SDK example file. I am quite new to Go language; I am not sure how to run below docker SDK API code. But I tried somehow; please help me with below error.

When I tried to run this go run example-ps.go

example-ps.go - content is below

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, "docker.io/library/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)
}

I am getting below error. How do I import those library files?

example-ps.go:8:3: cannot find package "github.com/docker/docker/api/types" in any of:
        /usr/local/go/src/github.com/docker/docker/api/types (from $GOROOT)
        /Users/air/go/src/github.com/docker/docker/api/types (from $GOPATH)
example-ps.go:9:3: cannot find package "github.com/docker/docker/api/types/container" in any of:
        /usr/local/go/src/github.com/docker/docker/api/types/container (from $GOROOT)
        /Users/air/go/src/github.com/docker/docker/api/types/container (from $GOPATH)
example-ps.go:7:3: cannot find package "github.com/docker/docker/client" in any of:
        /usr/local/go/src/github.com/docker/docker/client (from $GOROOT)
        /Users/air/go/src/github.com/docker/docker/client (from $GOPATH)
example-ps.go:10:3: cannot find package "golang.org/x/net/context" in any of:
        /usr/local/go/src/golang.org/x/net/context (from $GOROOT)
        /Users/air/go/src/golang.org/x/net/context (from $GOPATH)
  • 写回答

1条回答 默认 最新

  • duande3134 2017-08-01 00:03
    关注

    if you have golang installed correctly just issue this in the dir where your golang code lives

    go get -v -t ./...
    

    which will install the upstream libraries ... then you can compile your code ... for details issue

    go help packages
    

    the short of it is ./... will install all packages [ and recursively its upstream packages, etc, etc.] mentioned in any go file's import in your current dir

    If you have ever spent days manually installing upstream libraries for any other language you will appreciate how much of a lifesaver this really is

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵