Network class

A Docker user-defined network managed by testcontainers-dart.

Networks are created lazily via create and removed via remove. The static use helper combines both operations with a try/finally guarantee, matching the context-manager pattern used in testcontainers-python.

Example:

await Network.use((network) async {
  final db = DockerContainer('postgres:16')
    ..withNetwork(network)
    ..withNetworkAliases(['db']);
  final app = DockerContainer('myapp:latest')
    ..withNetwork(network);
  // db is reachable from app at hostname 'db'
  await DockerContainer.use(db, (_) async {
    await DockerContainer.use(app, (c) async { ... });
  });
});

Constructors

Network({DockerClient? dockerClient})
Creates a Network with a randomly generated name.

Properties

hashCode int
The hash code for this object.
no setterinherited
id String?
The Docker-assigned network ID, or null before create is called.
no setter
name String
The unique name assigned to this network.
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

connect(String containerId, {List<String>? networkAliases}) Future<void>
Attaches containerId to this network.
create() Future<Network>
Creates the Docker network and returns this.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
remove() Future<void>
Removes the Docker network.
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited

Static Methods

use<T>(Future<T> fn(Network)) Future<T>
Creates a network, runs fn with it, and removes the network afterwards.