Skip to content

Commit 3bafc47

Browse files
authored
Support custom compose project names (#800)
1 parent 693ea8c commit 3bafc47

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

docs/features/compose.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,16 @@ const environment = await new DockerComposeEnvironment(composeFilePath, composeF
121121
.up();
122122
```
123123

124+
### With custom project name
125+
126+
See [project name](https://docs.docker.com/compose/project-name/).
127+
128+
```javascript
129+
const environment = await new DockerComposeEnvironment(composeFilePath, composeFile)
130+
.withProjectName("test")
131+
.up();
132+
```
133+
124134
## Downing a Docker compose environment
125135

126136
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.

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import fetch from "node-fetch";
22
import path from "path";
33
import { DockerComposeEnvironment } from "./docker-compose-environment";
4+
import { RandomUuid } from "../common";
45
import { Wait } from "../wait-strategies/wait";
56
import { PullPolicy } from "../utils/pull-policy";
67
import {
@@ -255,4 +256,15 @@ describe("DockerComposeEnvironment", () => {
255256

256257
await startedEnvironment.down();
257258
});
259+
260+
it("should use a custom project name if set", async () => {
261+
const customProjectName = `custom-${new RandomUuid().nextUuid()}`;
262+
const startedEnvironment = await new DockerComposeEnvironment(fixtures, "docker-compose.yml")
263+
.withProjectName(customProjectName)
264+
.up();
265+
266+
expect(await getRunningContainerNames()).toContain(`${customProjectName}-container-1`);
267+
268+
await startedEnvironment.down();
269+
});
258270
});

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ export class DockerComposeEnvironment {
7373
return this;
7474
}
7575

76+
public withProjectName(projectName: string): this {
77+
this.projectName = projectName;
78+
return this;
79+
}
80+
7681
public async up(services?: Array<string>): Promise<StartedDockerComposeEnvironment> {
7782
log.info(`Starting DockerCompose environment "${this.projectName}"...`);
7883
const client = await getContainerRuntimeClient();

0 commit comments

Comments
 (0)