Home > other >  How to use docker cli in golang to create subnet
How to use docker cli in golang to create subnet

Time:08-17

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

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?

CodePudding user response:

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

CodePudding user response:

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:

networkResponse, err := client.NetworkCreate(context.Background(), "mynet", types.NetworkCreate{
    Driver: "bridge",
    IPAM: &network.IPAM{
        Config: network.IPAMConfig{
            Subnet: "174.3.12.5/16",
        },
    },
})
  • Related