为Docker中的容器设置端口,使用Docker Golang API。

huangapple go评论122阅读模式
英文:

setting ports for container in docker for docker golang api

问题

我期待使用Docker Golang API来执行以下操作:

命令: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(客户端和服务器都是)。

这是我正在使用的代码片段:

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/docker/docker/client"
  5. "github.com/docker/docker/api/types"
  6. "github.com/docker/docker/api/types/container"
  7. "golang.org/x/net/context"
  8. "github.com/docker/go-connections/nat"
  9. )
  10. func check(err error) {
  11. if err != nil {
  12. panic(err)
  13. }
  14. }
  15. func main() {
  16. ctx := context.Background()
  17. cli, err := client.NewEnvClient()
  18. check(err)
  19. config := &container.Config{
  20. Image: "image-name",
  21. ExposedPorts: nat.PortSet{
  22. "8080/tcp": struct{}{},
  23. },
  24. Cmd: []string{"sh", "-c", "while true; do sleep always; done", "/bin/bash"},
  25. }
  26. hostConfig := &container.HostConfig{
  27. PortBindings: nat.PortMap{
  28. "8080/tcp": []nat.PortBinding{
  29. {
  30. HostIP: "0.0.0.0",
  31. HostPort: "8989",
  32. },
  33. },
  34. },
  35. }
  36. resp, err := cli.ContainerCreate(ctx, config, hostConfig, nil, "")
  37. check(err)
  38. if err := cli.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{}); err != nil {
  39. panic(err)
  40. }
  41. }

编译这段代码后,我得到以下错误:

  1. # command-line-arguments
  2. 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
  3. 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的初学者。

英文:

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

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/docker/docker/client"
  5. "github.com/docker/docker/api/types"
  6. "github.com/docker/docker/api/types/container"
  7. "golang.org/x/net/context"
  8. "github.com/docker/go-connections/nat"
  9. //"github.com/docker/docker/vendor/github.com/docker/go-connections/nat"
  10. )
  11. func check(err error) {
  12. if err != nil {
  13. panic(err)
  14. }
  15. }
  16. func main(){
  17. ctx := context.Background()
  18. cli, err := client.NewEnvClient()
  19. check(err)
  20. config := &container.Config{
  21. Image : image-name,
  22. ExposedPorts: nat.PortSet{
  23. "8080/tcp": struct{}{},
  24. },
  25. Cmd : [] string {"sh","-c","while true; do sleep always; done","/bin/bash"},
  26. }
  27. host_config := &container.HostConfig{
  28. PortBindings: nat.PortMap{
  29. "8080/tcp": []nat.PortBinding{
  30. {
  31. HostIP: "0.0.0.0",
  32. HostPort: "8989",
  33. },
  34. },
  35. },
  36. }
  37. resp, err := cli.ContainerCreate(ctx,config,host_config, nil,"")
  38. check(err)
  39. if err := cli.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{});
  40. err != nil {
  41. panic(err)
  42. }
  43. }

After Compiling this code I get the following error

  1. # command-line-arguments
  2. 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
  3. 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.

答案1

得分: 1

这是一个关于vendor/工作方式的Golang问题。

删除嵌套的vendor目录:

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

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

更多详情,请查看这个已报告的问题

英文:

This is a Golang issue with how vendor/ works.

Remove the nested vendor directory:

  1. 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

答案2

得分: -1

我的OSX解决方案:

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

My solution for OSX:

  1. mv /Users/&lt;user&gt;/go/src/github.com/docker/docker/vendor/github.com/docker/go-connections/{nat,nat.old}

huangapple
  • 本文由 发表于 2017年8月7日 16:03:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/45542071.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定