use<T> static method

Future<T> use<T>(
  1. DockerContainer container,
  2. Future<T> fn(
    1. DockerContainer
    )
)

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();
  }
}