duankuiyu4618 2017-08-07 08:03
浏览 149

为docker golang api在docker中设置容器的端口

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.

  • 写回答

2条回答 默认 最新

  • duangan6797 2017-08-07 08:35
    关注

    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

    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料