NetworkDefinition Class
Describes how a network should be created. Configure an instance and call NetworkDefinition.CreateNetwork to obtain a TemporaryNetwork.
public sealed class NetworkDefinitionExample
await using var network = new NetworkDefinition().CreateNetwork();
var redisDefinition = new ContainerDefinition(ImageSource.FromRegistry("redis:8"));
redisDefinition.Network.Add(network, "redis");
await using var redis = redisDefinition.CreateContainer();
await redis.StartAsync();
// The second container reaches the first one by its alias.
var clientDefinition = new ContainerDefinition(ImageSource.FromRegistry("redis:8"));
clientDefinition.Network.Add(network);
clientDefinition.Command.Add("redis-cli");
clientDefinition.Command.Add("-h");
clientDefinition.Command.Add("redis");
clientDefinition.Command.Add("ping");
await using var client = clientDefinition.CreateContainer();
await client.StartAsync();Constructors
| Name | Description |
|---|---|
| NetworkDefinition() | Initializes a new instance of the NetworkDefinition class. |
| NetworkDefinition(NetworkDefinition) | Initializes a new instance of the NetworkDefinition class by deep-copying another definition. The copy can be changed even when other belongs to a network. |
NetworkDefinition overloads
NetworkDefinition()
NetworkDefinition.csInitializes a new instance of the NetworkDefinition class.
public NetworkDefinition()NetworkDefinition(NetworkDefinition)
NetworkDefinition.csInitializes a new instance of the NetworkDefinition class by deep-copying another definition. The copy can be changed even when other belongs to a network.
public NetworkDefinition(NetworkDefinition other)Parameters
otherNetworkDefinition- The definition to copy.
Properties
| Name | Description |
|---|---|
| Driver | Gets or sets the network driver. When null, the runtime uses its default driver: bridge, or nat on a daemon that runs Windows containers. |
| Internal | Gets or sets a value indicating whether the network is isolated from the outside: its containers reach each other, but not the internet. |
| IsReadOnly | Gets a value indicating whether the definition belongs to a network, in which case it can no longer be changed. |
| Labels | Gets the labels. |
| Name | Gets or sets the network name. When null, a random name is generated, or a name derived from NetworkDefinition.ReuseId when that is set. |
| ReuseId | Gets or sets an identifier used to reuse an existing network across runs. When set, the network is not removed on dispose. |
| Runtime | Gets or sets the container runtime to use. |
Driver
NetworkDefinition.csInternal
NetworkDefinition.csIsReadOnly
NetworkDefinition.csLabels
NetworkDefinition.csGets or sets the network name. When null, a random name is generated, or a name derived from NetworkDefinition.ReuseId when that is set.
public string? Name { get; set; }Property value
ReuseId
NetworkDefinition.csRuntime
NetworkDefinition.csGets or sets the container runtime to use.
public ContainerRuntime Runtime { get; set; }Property value
Methods
| Name | Description |
|---|---|
| CreateNetwork() | Creates a TemporaryNetwork from a deep copy of this definition. Later changes to this definition do not affect the returned network, and the copy the network owns cannot be changed. |
CreateNetwork()
NetworkDefinition.csCreates a TemporaryNetwork from a deep copy of this definition. Later changes to this definition do not affect the returned network, and the copy the network owns cannot be changed.
public TemporaryNetwork CreateNetwork()Returns
TemporaryNetwork A new network.