Skip to content

Commit

Permalink
Support custom compose project names (#800)
Browse files Browse the repository at this point in the history
  • Loading branch information
jalaziz authored Jul 26, 2024
1 parent 693ea8c commit 3bafc47
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
10 changes: 10 additions & 0 deletions docs/features/compose.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,16 @@ const environment = await new DockerComposeEnvironment(composeFilePath, composeF
.up();
```

### With custom project name

See [project name](https://docs.docker.com/compose/project-name/).

```javascript
const environment = await new DockerComposeEnvironment(composeFilePath, composeFile)
.withProjectName("test")
.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
@@ -1,6 +1,7 @@
import fetch from "node-fetch";
import path from "path";
import { DockerComposeEnvironment } from "./docker-compose-environment";
import { RandomUuid } from "../common";
import { Wait } from "../wait-strategies/wait";
import { PullPolicy } from "../utils/pull-policy";
import {
Expand Down Expand Up @@ -255,4 +256,15 @@ describe("DockerComposeEnvironment", () => {

await startedEnvironment.down();
});

it("should use a custom project name if set", async () => {
const customProjectName = `custom-${new RandomUuid().nextUuid()}`;
const startedEnvironment = await new DockerComposeEnvironment(fixtures, "docker-compose.yml")
.withProjectName(customProjectName)
.up();

expect(await getRunningContainerNames()).toContain(`${customProjectName}-container-1`);

await startedEnvironment.down();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ export class DockerComposeEnvironment {
return this;
}

public withProjectName(projectName: string): this {
this.projectName = projectName;
return this;
}

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

0 comments on commit 3bafc47

Please sign in to comment.