How to use docker cli in golang to create subnet

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

How to use docker cli in golang to create subnet

问题

我正在尝试为我用Golang编写的应用程序创建一个Docker网络。

我知道可以使用这个NetworkCreate函数,但我不确定如何指定网络选项。

在常规终端控制台中,我可以使用以下命令创建网络:

  1. docker network create -d bridge --subnet=174.3.12.5/16 mynet

但如何使用NetworkCreate()函数来等效地创建这个网络呢?

英文:

I'm trying to create a docker network for my application written in Golang.

I'm aware that I can use this NetworkCreate function, but I'm not sure how to specify the network option.

In the regular terminal console, I can just create the network with

  1. docker network create -d bridge --subnet=174.3.12.5/16 mynet

But how to use the NetworkCreate() as an equivalent for this network creation?

答案1

得分: 1

  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/docker/docker/api/types"
  6. "github.com/docker/docker/api/types/network"
  7. "github.com/docker/docker/client"
  8. )
  9. func main() {
  10. cli, err := client.NewClientWithOpts(client.FromEnv)
  11. if err != nil {
  12. fmt.Println(err)
  13. }
  14. newnetwork := types.NetworkCreate{IPAM: &network.IPAM{
  15. Driver: "default",
  16. Config: []network.IPAMConfig{network.IPAMConfig{
  17. Subnet: "174.3.12.5/16",
  18. }},
  19. }}
  20. res, err := cli.NetworkCreate(context.Background(), "test", newnetwork)
  21. if err != nil {
  22. fmt.Println(err)
  23. return
  24. }
  25. fmt.Println(res)
  26. }

这是一个最小可实现的示例。驱动程序的名称是 default

英文:
  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/docker/docker/api/types"
  6. "github.com/docker/docker/api/types/network"
  7. "github.com/docker/docker/client"
  8. )
  9. func main() {
  10. cli, err := client.NewClientWithOpts(client.FromEnv)
  11. if err != nil {
  12. fmt.Println(err)
  13. }
  14. newnetwork := types.NetworkCreate{IPAM: &network.IPAM{
  15. Driver: "default",
  16. Config: []network.IPAMConfig{network.IPAMConfig{
  17. Subnet: "174.3.12.5/16",
  18. }},
  19. }}
  20. res, err := cli.NetworkCreate(context.Background(), "test", newnetwork)
  21. if err != nil {
  22. fmt.Println(err)
  23. return
  24. }
  25. fmt.Println(res)
  26. }

this is a minimal implementable example. the name for driver is default.

答案2

得分: 0

您可以通过NetworkCreate选项结构来指定网络选项。

如果您想将命令docker network create -d bridge --subnet=174.3.12.5/16 mynet转换为等效的Go语言代码,可以像这样编写:

  1. networkResponse, err := client.NetworkCreate(context.Background(), "mynet", types.NetworkCreate{
  2. Driver: "bridge",
  3. IPAM: &network.IPAM{
  4. Config: []network.IPAMConfig{
  5. {
  6. Subnet: "174.3.12.5/16",
  7. },
  8. },
  9. },
  10. })
英文:

You can specify the network options via the NetworkCreate options struct.

If you want to convert the command docker network create -d bridge --subnet=174.3.12.5/16 mynet to a golang equivalent, It'll look something like this:

  1. networkResponse, err := client.NetworkCreate(context.Background(), "mynet", types.NetworkCreate{
  2. Driver: "bridge",
  3. IPAM: &network.IPAM{
  4. Config: network.IPAMConfig{
  5. Subnet: "174.3.12.5/16",
  6. },
  7. },
  8. })

答案3

得分: -2

你可以使用exec.Command(...).CombinedOutput()

英文:

You can use exec.Command(...).CombinedOutput()

huangapple
  • 本文由 发表于 2022年8月17日 12:58:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/73383055.html
匿名

发表评论

匿名网友

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

确定