Skip to content

Commit ed0fbbf

Browse files
committed
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.
1 parent e4fa915 commit ed0fbbf

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

packages/testcontainers/src/docker-compose-environment/docker-compose-environment.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,16 @@ describe("DockerComposeEnvironment", { timeout: 180_000 }, () => {
9898
await startedEnvironment.down();
9999
});
100100

101+
it("should support default wait strategy", async () => {
102+
const startedEnvironment = await new DockerComposeEnvironment(fixtures, "docker-compose-with-healthcheck.yml")
103+
.withDefaultWaitStrategy(Wait.forHealthCheck())
104+
.up();
105+
106+
await checkEnvironmentContainerIsHealthy(startedEnvironment, await composeContainerName("container"));
107+
108+
await startedEnvironment.down();
109+
});
110+
101111
it("should support log message wait strategy", async () => {
102112
const startedEnvironment = await new DockerComposeEnvironment(fixtures, "docker-compose.yml")
103113
.withWaitStrategy(await composeContainerName("container"), Wait.forLogMessage("Listening on port 8080"))

packages/testcontainers/src/docker-compose-environment/docker-compose-environment.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export class DockerComposeEnvironment {
2323
private profiles: string[] = [];
2424
private environment: Environment = {};
2525
private pullPolicy: ImagePullPolicy = PullPolicy.defaultPolicy();
26+
private defaultWaitStrategy: WaitStrategy = Wait.forListeningPorts();
2627
private waitStrategy: { [containerName: string]: WaitStrategy } = {};
2728
private startupTimeout?: number;
2829

@@ -63,6 +64,11 @@ export class DockerComposeEnvironment {
6364
return this;
6465
}
6566

67+
public withDefaultWaitStrategy(waitStrategy: WaitStrategy): this {
68+
this.defaultWaitStrategy = waitStrategy;
69+
return this;
70+
}
71+
6672
public withWaitStrategy(containerName: string, waitStrategy: WaitStrategy): this {
6773
this.waitStrategy[containerName] = waitStrategy;
6874
return this;
@@ -140,7 +146,7 @@ export class DockerComposeEnvironment {
140146
const boundPorts = BoundPorts.fromInspectResult(client.info.containerRuntime.hostIps, mappedInspectResult);
141147
const waitStrategy = this.waitStrategy[containerName]
142148
? this.waitStrategy[containerName]
143-
: Wait.forListeningPorts();
149+
: this.defaultWaitStrategy;
144150
if (this.startupTimeout !== undefined) {
145151
waitStrategy.withStartupTimeout(this.startupTimeout);
146152
}

0 commit comments

Comments
 (0)