NetworkDefinition Class

Describes how a network should be created. Configure an instance and call NetworkDefinition.CreateNetwork to obtain a TemporaryNetwork.

public sealed class NetworkDefinition

Example

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

NameDescription
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.cs

Initializes a new instance of the NetworkDefinition class.

public NetworkDefinition()

NetworkDefinition(NetworkDefinition)

NetworkDefinition.cs

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.

public NetworkDefinition(NetworkDefinition other)
Parameters
other NetworkDefinition
The definition to copy.

Properties

NameDescription
DriverGets or sets the network driver. When null, the runtime uses its default driver: bridge, or nat on a daemon that runs Windows containers.
InternalGets or sets a value indicating whether the network is isolated from the outside: its containers reach each other, but not the internet.
IsReadOnlyGets a value indicating whether the definition belongs to a network, in which case it can no longer be changed.
LabelsGets the labels.
NameGets or sets the network name. When null, a random name is generated, or a name derived from NetworkDefinition.ReuseId when that is set.
ReuseIdGets or sets an identifier used to reuse an existing network across runs. When set, the network is not removed on dispose.
RuntimeGets or sets the container runtime to use.

Gets or sets the network driver. When null, the runtime uses its default driver: bridge, or nat on a daemon that runs Windows containers.

public string? Driver { get; set; }

Property value

string?

Remarks

Apple's container runtime does not support network drivers.

Gets or sets a value indicating whether the network is isolated from the outside: its containers reach each other, but not the internet.

public bool Internal { get; set; }

Property value

bool

Gets a value indicating whether the definition belongs to a network, in which case it can no longer be changed.

public bool IsReadOnly { get; }

Property value

bool

Gets the labels.

public ContainerLabelCollection Labels { get; }

Gets 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

string?

Gets or sets an identifier used to reuse an existing network across runs. When set, the network is not removed on dispose.

public string? ReuseId { get; set; }

Property value

string?

Gets or sets the container runtime to use.

public ContainerRuntime Runtime { get; set; }

Property value

ContainerRuntime

Methods

NameDescription
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.cs

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.

public TemporaryNetwork CreateNetwork()

Returns

TemporaryNetwork A new network.