【问题标题】:setting ports for container in docker for docker golang api在 docker 中为 docker golang api 设置容器端口
【发布时间】:2017-08-07 08:03:04
【问题描述】:

我期待使用 docker golang api 在下面做这样的事情

cmd : docker run -t -i -p 8989:8080 "image-name" /bin/bash

我也在使用 golang sdk https://github.com/moby/moby/client 或 https://godoc.org/github.com/moby/moby/client 并且我的 docker api 版本是 1.30(客户端和服务器)

这是我正在使用的一段代码

 package main

 import (
       "fmt"

       "github.com/docker/docker/client"
       "github.com/docker/docker/api/types"
       "github.com/docker/docker/api/types/container"
       "golang.org/x/net/context"
       "github.com/docker/go-connections/nat"
    //"github.com/docker/docker/vendor/github.com/docker/go-connections/nat"
)

func check(err error) {
   if err != nil {
       panic(err)
   }
}


func main(){
    ctx := context.Background()
cli, err := client.NewEnvClient()
check(err)


config := &container.Config{
Image : image-name,
ExposedPorts: nat.PortSet{
    "8080/tcp": struct{}{},
},
Cmd : [] string {"sh","-c","while true; do sleep always; done","/bin/bash"},
}

host_config := &container.HostConfig{
     PortBindings: nat.PortMap{
     "8080/tcp": []nat.PortBinding{
         {
             HostIP: "0.0.0.0",
             HostPort: "8989",
         },
      },
   },
}


  resp, err := cli.ContainerCreate(ctx,config,host_config, nil,"")
  check(err)


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


}

编译这段代码后出现以下错误

# command-line-arguments
src\main\createcontainer1.go:53: cannot use "github.com/docker/go-connections/nat".PortSet literal (type "github.com/docker/go-connections/nat".PortSet) as type "github.com/docker/docker/vendor/github.com/docker/go-connections/nat".PortSet in field value

src\main\createcontainer1.go:65: cannot use "github.com/docker/go-connections/nat".PortMap literal (type "github.com/docker/go-connections/nat".PortMap) as type "github.com/docker/docker/vendor/github.com/docker/go-connections/nat".PortMap in field value

如果有人知道可能是什么问题以及如何解决它。 请回答它,因为我是 docker 的初学者。

【问题讨论】:

    标签: docker go docker-api


    【解决方案1】:

    这是 vendor/ 工作方式的 Golang 问题。

    删除嵌套的供应商目录:

    rm -rf vendor/github.com/docker/docker/vendor
    

    如果你使用glide,你应该在安装依赖时使用glide install -v。

    更多详情,请查看此报道issue

    【讨论】:

    • 我没有使用滑翔。反正没有它吗?我找不到上面提到的供应商目录...我系统中的目录是 C:\Projects\Go\pkg\windows_amd64\github.com\docker\docker\vendor\github.com\docker 并且该目录有go-connections 文件夹。请提出建议..
    • 实际上,docker 使用 vendoring。您可以尝试删除vendor 目录吗? - 错误引用它 - cannot use "github.com/docker/go-connections/nat".PortSet literal (type "github.com/docker/go-connections/nat".PortSet) as type "github.com/docker/docker/vendor/github.com/docker/go-connections/nat".PortSet in field value
    • 我删除了供应商目录,最终在 src\github.com\docker\docker\pkg\system\syscall_windows.go:6:2: cannot find package "github.com/ Sirupsen/logrus" 在以下任何位置:C:\Go\src\github.com\Sirupsen\logrus(来自 $GOROOT) C:\Projects\Go\src\github.com\Sirupsen\logrus(来自 $GOPATH)src\ github.com\docker\docker\client\container_commit.go:8:2: 在 C:\Go\src\github.com\docker\distribution 中找不到包“github.com/docker/distribution/reference” \reference (from $GOROOT) 和类似的更多类似的,请指导我修复它。
    • 对不起... :( 您可以从 https://github.com/moby/moby/vendor 恢复 vendor 目录(moby 是 docker 的新存储库),或者可能从您的克隆副本中恢复。你是如何安装这个 docker 包的第一名?
    • @ShmulikKlein 我有完全相同的问题,我通过“go get -u”安装了软件包
    【解决方案2】:

    我的 OSX 解决方案:

    mv /Users/<user>/go/src/github.com/docker/docker/vendor/github.com/docker/go-connections/{nat,nat.old}
    

    【讨论】:

    • 您需要更多解释如何解决他们的问题。
    猜你喜欢
    • 2018-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多