Example of using the Network config class when creating a container
It would be good to have an example of using the NetworkConfig to show how to add a container to a created network as this is something I'm struggling with.
I did the following:
CreateContainerParameters create = new CreateContainerParameters();
create.HostConfig = new HostConfig();
create.HostConfig.NetworkMode = "NetworkNameCreatedByYou";
Thanks, I’ll try that!
From: Daniel Gama [email protected] Sent: 13 August 2018 21:43 To: Microsoft/Docker.DotNet [email protected] Cc: Sean Farrow [email protected]; Author [email protected] Subject: Re: [Microsoft/Docker.DotNet] Example of using the Network config class when creating a container (#331)
I did the following:
CreateContainerParameters create = new CreateContainerParameters();
create.HostConfig = new HostConfig();
create.HostConfig.NetworkMode = "NetworkNameCreatedByYou";
— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHubhttps://github.com/Microsoft/Docker.DotNet/issues/331#issuecomment-412657030, or mute the threadhttps://github.com/notifications/unsubscribe-auth/ABY1fv2feHNsCaebiIWqbW4kCZdhN_DNks5uQeS_gaJpZM4V5jIn.
Hello! I have a question:
In Docker Compose you can specify multiple networks for a container to join to. Here, it seems we can just set only one? Or is NetworkMode a comma-separated list of networks to join to?
Thanks!
@darkguy2008 good question. I'm also waiting for the answer.
Simply add your network to NetworkingConfig and assign it to CreateContainerParameters. EndpointsConfig receives a dictionary with the network name as key and and instance of EndpointSettings as value Set EndpointSettings.NetworkID too.
@HofmeisterAn I've done something like this basing on your suggestion:
var parameters = new CreateContainerParameters
{
Name = $"incoming.api-{fileName}",
Image = "incoming.api:latest",
HostConfig = new HostConfig
{
AutoRemove = true
},
NetworkingConfig = new NetworkingConfig
{
EndpointsConfig = new Dictionary<string, EndpointSettings>
{
{"eltororojo_incoming-api-network", new EndpointSettings()},
{"eltororojo_itemdb-network", new EndpointSettings()}
}
}
};
I'm getting the following error when I'm trying to assign container to multiple networks on creation:
Unhandled exception. Docker.DotNet.DockerApiException: Docker API responded with status code=BadRequest, response={"message":"Container cannot be connected to network endpoints: eltororojo_incoming-api-network, eltororojo_itemdb-network"}
I've found the possible cause in the following thread on SO: https://stackoverflow.com/questions/60301221/docker-container-cannot-be-connected-to-network-endpoints What is the common practise in such scenario? Should I connect container to only one network and then after it's creation connect to the rest with e.g. exec?
You're missing the NetworkID as mentioned above. It will be something like:
_ = new KeyValuePair<string, EndpointSettings>("18097689-b678-419a-863e-f0eefb1864cc", new EndpointSettings { NetworkID = "97bbb933b398b9ee8492007112b46063c1b6c4749885dca56ad9f4a8cf2db9e4" });
Notice the Name and Id property of docker inspect:
[
{
"Name":"18097689-b678-419a-863e-f0eefb1864cc",
"Id":"97bbb933b398b9ee8492007112b46063c1b6c4749885dca56ad9f4a8cf2db9e4",
"Created":"2022-12-05T13:49:13.684424004Z",
"Scope":"local",
"Driver":"bridge",
"EnableIPv6":false,
"IPAM":{
"Driver":"default",
"Options":null,
"Config":[
{
"Subnet":"172.18.0.0/16",
"Gateway":"172.18.0.1"
}
]
},
"Internal":false,
"Attachable":false,
"Ingress":false,
"ConfigFrom":{
"Network":""
},
"ConfigOnly":false,
"Containers":{
},
"Options":{
},
"Labels":{
"org.testcontainers":"true",
"org.testcontainers.lang":"dotnet",
"org.testcontainers.resource-reaper-session":"c2b489f7-1ff1-4132-b1bd-ee63b3e341b6",
"org.testcontainers.version":"2.3.0+a02e1a405f4d5b47dbb291c89c1c1472e3c1a1ff"
}
}
]