为docker golang api的docker设置容器的端口 [英] setting ports for container in docker for docker golang api

查看:1091
本文介绍了为docker golang api的docker设置容器的端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我期待着使用docker golang api来做下面的事情。


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



另外,我正在使用golang sdk https://github.com/moby/moby/client https ://godoc.org/github.com/moby/moby/client 和我的docker api版本是1.30(Client& Server both)



这里是我使用的代码片段
$ b $ pre $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $'

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检查(错误错误){
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;总是睡觉;完成,/ 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)
}


}

编译此代码后我收到以下错误:

pre $ #命令行参数
src\main\createcontainer1.go:53 :不能使用github.com/docker/go-connections/nat\".PortSet文字(键入github.com/docker/go-connections/nat\".PortSet)类型为github.com/docker/docker/vendor/ github.com/docker/go-connections/nat\".PortSet in field value

src\mai n \createcontainer1.go:65:不能使用github.com/docker/go-connections/nat\".PortMap文字(输入github.com/docker/go-connections/nat\".PortMap)作为类型github。 com / docker / docker / vendor / github.com / docker / go-connections / nat.PortMap in field value

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

解决方案

这是一个关于 vendor / 工作原理的Golang问题。 / p>

删除嵌套的供应商目录:

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

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



有关更多详细信息,检查此报告的问题


I am looking forward to do something below like this using docker golang api

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

Also I am using golang sdk https://github.com/moby/moby/client or https://godoc.org/github.com/moby/moby/client and my docker api version is 1.30 (Client & Server both)

Here is the piece of code I am using

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


}

After Compiling this code I get the following error

# 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

If somebody knows what could be the problem and how to fix it. Please answer to it as I am beginner with docker.

解决方案

This is a Golang issue with how vendor/ works.

Remove the nested vendor directory:

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

If you are using glide, you should use glide install -v when installing the dependency.

For more details, check this reported issue

这篇关于为docker golang api的docker设置容器的端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆