From ed0fbbf4e9669e061840f656835da91075a10ae9 Mon Sep 17 00:00:00 2001 From: Jameel Al-Aziz Date: Wed, 16 Apr 2025 18:20:51 -0400 Subject: [PATCH 1/2] Allow overriding default compose wait strategy Allow the default wait strategy to be overriden for docker compose environments. The default "listening ports" strategy doesn't work for distroless images and in complex setups, having to explicitly override the wait strategy for each service is error-prone. --- .../docker-compose-environment.test.ts | 10 ++++++++++ .../docker-compose-environment.ts | 8 +++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/packages/testcontainers/src/docker-compose-environment/docker-compose-environment.test.ts b/packages/testcontainers/src/docker-compose-environment/docker-compose-environment.test.ts index e59c8dfd3..d97c604db 100644 --- a/packages/testcontainers/src/docker-compose-environment/docker-compose-environment.test.ts +++ b/packages/testcontainers/src/docker-compose-environment/docker-compose-environment.test.ts @@ -98,6 +98,16 @@ describe("DockerComposeEnvironment", { timeout: 180_000 }, () => { await startedEnvironment.down(); }); + it("should support default wait strategy", async () => { + const startedEnvironment = await new DockerComposeEnvironment(fixtures, "docker-compose-with-healthcheck.yml") + .withDefaultWaitStrategy(Wait.forHealthCheck()) + .up(); + + await checkEnvironmentContainerIsHealthy(startedEnvironment, await composeContainerName("container")); + + await startedEnvironment.down(); + }); + it("should support log message wait strategy", async () => { const startedEnvironment = await new DockerComposeEnvironment(fixtures, "docker-compose.yml") .withWaitStrategy(await composeContainerName("container"), Wait.forLogMessage("Listening on port 8080")) diff --git a/packages/testcontainers/src/docker-compose-environment/docker-compose-environment.ts b/packages/testcontainers/src/docker-compose-environment/docker-compose-environment.ts index 83dd0a2cb..f63f32640 100644 --- a/packages/testcontainers/src/docker-compose-environment/docker-compose-environment.ts +++ b/packages/testcontainers/src/docker-compose-environment/docker-compose-environment.ts @@ -23,6 +23,7 @@ export class DockerComposeEnvironment { private profiles: string[] = []; private environment: Environment = {}; private pullPolicy: ImagePullPolicy = PullPolicy.defaultPolicy(); + private defaultWaitStrategy: WaitStrategy = Wait.forListeningPorts(); private waitStrategy: { [containerName: string]: WaitStrategy } = {}; private startupTimeout?: number; @@ -63,6 +64,11 @@ export class DockerComposeEnvironment { return this; } + public withDefaultWaitStrategy(waitStrategy: WaitStrategy): this { + this.defaultWaitStrategy = waitStrategy; + return this; + } + public withWaitStrategy(containerName: string, waitStrategy: WaitStrategy): this { this.waitStrategy[containerName] = waitStrategy; return this; @@ -140,7 +146,7 @@ export class DockerComposeEnvironment { const boundPorts = BoundPorts.fromInspectResult(client.info.containerRuntime.hostIps, mappedInspectResult); const waitStrategy = this.waitStrategy[containerName] ? this.waitStrategy[containerName] - : Wait.forListeningPorts(); + : this.defaultWaitStrategy; if (this.startupTimeout !== undefined) { waitStrategy.withStartupTimeout(this.startupTimeout); } From 8a1cba72d4eaf7cba1efe4669d3a9660a2959bf8 Mon Sep 17 00:00:00 2001 From: Jameel Al-Aziz Date: Mon, 21 Apr 2025 16:53:54 -0700 Subject: [PATCH 2/2] Add docs for compose default wait strategy. --- docs/features/compose.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/docs/features/compose.md b/docs/features/compose.md index 9fbc066fe..9ffa9860a 100644 --- a/docs/features/compose.md +++ b/docs/features/compose.md @@ -41,6 +41,17 @@ const environment = await new DockerComposeEnvironment(composeFilePath, composeF .up(); ``` +### With a default wait strategy + +By default Testcontainers uses the "listening ports" wait strategy for all containers. If you'd like to override +the default wait strategy for all services, you can do so: + +```javascript +const environment = await new DockerComposeEnvironment(composeFilePath, composeFile) + .withDefaultWaitStrategy(Wait.forHealthCheck()) + .up(); +``` + ### With a pull policy Testcontainers will automatically pull an image if it doesn't exist. This is configurable: