use<T> static method

Future<T> use<T>(
  1. DockerImage image,
  2. Future<T> fn(
    1. DockerImage
    )
)

Builds image, runs fn with it, and removes it afterwards.

The image is removed even if fn throws. This is the recommended way to use a custom image in tests:

await DockerImage.use(
  DockerImage(path: './fixtures/nginx', tag: 'test-nginx:latest'),
  (image) async {
    await DockerContainer.use(DockerContainer(image.tag!), (c) async {
      // test against c
    });
  },
);

Implementation

static Future<T> use<T>(
  DockerImage image,
  Future<T> Function(DockerImage) fn,
) async {
  await image.build();
  try {
    return await fn(image);
  } finally {
    await image.remove();
  }
}