Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions docs/features/compose.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,17 @@ const environment = await new DockerComposeEnvironment(composeFilePath, composeF
.up();
```

### With custom client options

See [docker-compose](https://github.com/PDMLab/docker-compose/) library.

```javascript
const environment = await new DockerComposeEnvironment(composeFilePath, composeFile)
.withClientOptions({ executable: { standalone: true, executablePath: "/path/to/docker-compose" } })
.up();
```


## Downing a Docker compose environment

Testcontainers by default will not wait until the environment has downed. It will simply issue the down command and return immediately. This is to save time when running tests.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@ export function defaultComposeOptions(
COMPOSE_PROJECT_NAME: options.projectName,
...{ ...environment, ...options.environment },
},
executable: options.executable,
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,21 @@ export type ComposeOptions = {
composeOptions?: string[];
environment?: NodeJS.ProcessEnv;
logger?: Logger;
executable?: ComposeExecutableOptions;
};

export type ComposeExecutableOptions =
| {
executablePath: string;
options?: string[] | (string | string[])[];
standalone?: never;
}
| {
executablePath?: string;
options?: never;
standalone: true;
};

export type ComposeDownOptions = {
timeout: number;
removeVolumes: boolean;
Expand Down
2 changes: 1 addition & 1 deletion packages/testcontainers/src/container-runtime/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export { getAuthConfig } from "./auth/get-auth-config";
export { ContainerRuntimeClient, getContainerRuntimeClient } from "./clients/client";
export { parseComposeContainerName } from "./clients/compose/parse-compose-container-name";
export { ComposeDownOptions, ComposeOptions } from "./clients/compose/types";
export { ComposeDownOptions, ComposeExecutableOptions, ComposeOptions } from "./clients/compose/types";
export { HostIp } from "./clients/types";
export { ImageName } from "./image-name";
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ContainerInfo } from "dockerode";
import { containerLog, log, RandomUuid, Uuid } from "../common";
import { getContainerRuntimeClient, parseComposeContainerName } from "../container-runtime";
import { ComposeOptions, getContainerRuntimeClient, parseComposeContainerName } from "../container-runtime";
import { StartedGenericContainer } from "../generic-container/started-generic-container";
import { getReaper } from "../reaper/reaper";
import { Environment } from "../types";
Expand All @@ -26,6 +26,7 @@ export class DockerComposeEnvironment {
private defaultWaitStrategy: WaitStrategy = Wait.forListeningPorts();
private waitStrategy: { [containerName: string]: WaitStrategy } = {};
private startupTimeoutMs?: number;
private clientOptions: Partial<ComposeOptions> = {};

constructor(composeFilePath: string, composeFiles: string | string[], uuid: Uuid = new RandomUuid()) {
this.composeFilePath = composeFilePath;
Expand Down Expand Up @@ -84,27 +85,41 @@ export class DockerComposeEnvironment {
return this;
}

public withClientOptions(
options: Partial<Omit<ComposeOptions, "filePath" | "files" | "projectName" | "environment">>
): this {
this.clientOptions = { ...this.clientOptions, ...options };
return this;
}

public async up(services?: Array<string>): Promise<StartedDockerComposeEnvironment> {
log.info(`Starting DockerCompose environment "${this.projectName}"...`);
const client = await getContainerRuntimeClient();
const reaper = await getReaper(client);
reaper.addComposeProject(this.projectName);

const {
composeOptions: clientComposeOptions = [],
commandOptions: clientCommandOptions = [],
...remainingClientOptions
} = this.clientOptions;

const options = {
...remainingClientOptions,
filePath: this.composeFilePath,
files: this.composeFiles,
projectName: this.projectName,
};

const commandOptions = [];
const commandOptions = [...clientCommandOptions];
if (this.build) {
commandOptions.push("--build");
}
if (!this.recreate) {
commandOptions.push("--no-recreate");
}

const composeOptions: string[] = [];
const composeOptions = [...clientComposeOptions];
if (this.environmentFile) {
composeOptions.push("--env-file", this.environmentFile);
}
Expand Down
Loading