Skip to content

Start a group of containers with dependencies and shared cleanup #1565

Open

Description

@cristianrgreco

What happens

A test that needs several containers wires them up by hand:

const network = await new Network().start();
const db = await new PostgreSqlContainer(dbImage).withNetwork(network).withNetworkAliases("db").start();
const app = await new GenericContainer(appImage)
  .withNetwork(network)
  .withEnvironment({ DATABASE_URL: "postgres://test:test@db:5432/test" })
  .withExposedPorts(8080)
  .start();
// ...
await app.stop();
await db.stop();
await network.stop();
  • Start order is whatever order the awaits are written in. Independent containers start one after another unless the test adds its own Promise.all.
  • With Promise.all, if one start() rejects the others keep running until Ryuk removes them at process exit.
  • Cleanup is one call per container, in the right order.

Expected

A way to declare a set of containers, their dependencies and a shared network, start them as a unit and dispose of them as a unit:

await using group = await new ContainerGroup()
  .with("db", new PostgreSqlContainer(dbImage))
  .with("app", new GenericContainer(appImage).withExposedPorts(8080), { dependsOn: ["db"] })
  .start();

const app = group.get("app");
  • Containers without a dependency between them start in parallel.
  • Each name becomes a network alias on a network the group owns.
  • If any container fails to start, the ones already started are stopped.

Where

New code in packages/testcontainers/src, built on GenericContainer and Network.

Notes

  • Java has dependsOn(Startable...) on containers and Startables.deepStart(...) for parallel start.
  • DockerComposeEnvironment covers this when the setup lives in a compose file. This is for setups defined in code, including module containers.
  • The API shape above is a sketch.

Dependencies

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestminorBackward compatible functionality

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions