为什么我无法为docker取sdk?

时间:2017-07-31 23:46:57

标签: docker go

我正在尝试运行docker go SDK示例文件。我对Go语言很陌生;我不知道如何运行docker SDK API代码。但我试着以某种方式;请帮我解决以下错误。

当我尝试运行此go run example-ps.go

example-ps.go - 内容低于

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)
}

我收到以下错误。如何导入这些库文件?

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 个答案:

答案 0 :(得分:2)

如果你正确安装了golang,只需在你的golang代码所在的目录中发出这个

go get -v -t ./...

将安装上游库...然后您可以编译您的代码...以获取详细信息

go help packages

缺点是./...将安装当前目录中任何go文件import中提到的所有包[并递归其上游包等等]

如果您花了几天时间为任何其他语言手动安装上游库,那么您将会体会到它的真正救命之处