use<T> static method
Starts container, runs fn with it, and stops it afterwards.
The container is stopped even if fn throws. This is the recommended
idiom for using containers in tests:
await DockerContainer.use(
DockerContainer('postgres:16').withExposedPorts([5432]),
(pg) async {
final port = await pg.exposedPort(5432);
// run tests
},
);
Implementation
static Future<T> use<T>(
DockerContainer container,
Future<T> Function(DockerContainer) fn,
) async {
await container.start();
try {
return await fn(container);
} finally {
await container.stop();
}
}