Reputation: 1719
I want to set up a docker network with C# using this client: https://github.com/dotnet/Docker.DotNet.
I am using docker for windows with windows containers.
Basically all I want to do is execute this command via the C# client (executing it in cmd works fine):
docker network create --driver=nat --subnet=10.123.174.0/23 --gateway=10.123.174.1 my_network
However, somehow I do not manage to set up the network properly. I use this small piece of code:
Dictionary<string, string> options = new Dictionary<string, string>();
options.Add("--gateway", "10.123.174.1");
options.Add("--subnet", "10.123.174.0/23");
await _client.Networks.CreateNetworkAsync(new NetworksCreateParameters { Driver = "nat", Name = "my_network", Options = options });
But the network still is created with another gateway than specified, putting my options only in the network "options" array:
[
{
"Name": "my_network",
"Id": "2bb2fabfa84518a2a11eff881c859bf324ce732c308a1d5a4d541c74870f5e70",
"Created": "2021-03-17T14:09:01.694993+01:00",
"Scope": "local",
"Driver": "nat",
"EnableIPv6": false,
"IPAM": {
"Driver": "windows",
"Options": null,
"Config": [
{
"Subnet": "172.29.96.0/20",
"Gateway": "172.29.96.1"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {},
"Options": {
"--gateway": "10.123.174.1",
"--subnet": "10.123.174.0/23",
"com.docker.network.windowsshim.hnsid": "3065184F-EFAF-4537-AF64-F2CD373A3261"
},
"Labels": {}
}
]
I also tried to specify just "subnet" and "gateway" (without the "--") but the result was the same. What do I do wrong?
Upvotes: 4
Views: 651
Reputation: 20276
As you may see from your own example, these settings belong to IPAM.Config (API reference), not Options:
"IPAM": {
"Config": [
{
"Subnet": "172.29.96.0/20",
"Gateway": "172.29.96.1"
}
]
},
You need to create an IPAM object and pass it to NetworksCreateParameters.IPAM argument. Here's a poor example:
Dictionary<string, string> options = new Dictionary<string, string>();
options.Add("Gateway", "10.123.174.1");
options.Add("Subnet", "10.123.174.0/23");
IList<Dictionary<string, string>> configs = new IList<Dictionary<string, string>>();
configs.Add(options);
Dictionary<string, IList<Dictionary<string, string>>> ipam = new Dictionary<string, IList<Dictionary<string, string>>>();
ipam.Add("Config", configs);
await _client.Networks.CreateNetworkAsync(new NetworksCreateParameters { Driver = "nat", Name = "my_network", IPAM = ipam });
Sorry, could've messed somewhere with the example as I only used C# a couple times in my life and that was years ago. Check out the API reference I mentioned. You can find there some examples on what data is expected.
Upvotes: 3