Skip to content

Commit cb89936

Browse files
authored
Add support for specifying container hostname (#889)
1 parent 15b62a1 commit cb89936

File tree

6 files changed

+37
-0
lines changed

6 files changed

+37
-0
lines changed

docs/features/containers.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,18 @@ const container = await new GenericContainer("alpine")
308308
.start();
309309
```
310310

311+
### With custom hostname
312+
313+
**Not recommended.**
314+
315+
See this [Docker blog post on Testcontainers best practices](https://www.docker.com/blog/testcontainers-best-practices/#:~:text=Don't%20hardcode%20the%20hostname)
316+
317+
```javascript
318+
const container = await new GenericContainer("alpine")
319+
.withHostname("my-hostname")
320+
.start();
321+
```
322+
311323
## Stopping a container
312324

313325
Testcontainers by default will not wait until the container has stopped. It will simply issue the stop command and return immediately. This is to save time when running tests.

packages/testcontainers/src/generic-container/abstract-started-container.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ export class AbstractStartedContainer implements StartedTestContainer {
3131
return this.startedTestContainer.getHost();
3232
}
3333

34+
public getHostname(): string {
35+
return this.startedTestContainer.getHostname();
36+
}
37+
3438
public getFirstMappedPort(): number {
3539
return this.startedTestContainer.getFirstMappedPort();
3640
}

packages/testcontainers/src/generic-container/generic-container.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,16 @@ describe("GenericContainer", () => {
534534
await secondStartedContainer.stop();
535535
});
536536

537+
it("should set the hostname", async () => {
538+
const container = await new GenericContainer("cristianrgreco/testcontainer:1.1.14")
539+
.withHostname("hostname")
540+
.start();
541+
542+
expect(container.getHostname()).toEqual("hostname");
543+
544+
await container.stop();
545+
});
546+
537547
// failing to build an image hangs within the DockerImageClient.build method,
538548
// that change might be larger so leave it out of this commit but skip the failing test
539549
it.skip("should throw an error for a target stage that does not exist", async () => {

packages/testcontainers/src/generic-container/generic-container.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,4 +475,9 @@ export class GenericContainer implements TestContainer {
475475
this.logConsumer = logConsumer;
476476
return this;
477477
}
478+
479+
public withHostname(hostname: string): this {
480+
this.createOpts.Hostname = hostname;
481+
return this;
482+
}
478483
}

packages/testcontainers/src/generic-container/started-generic-container.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ export class StartedGenericContainer implements StartedTestContainer {
8989
return this.host;
9090
}
9191

92+
public getHostname(): string {
93+
return this.inspectResult.Config.Hostname;
94+
}
95+
9296
public getFirstMappedPort(): number {
9397
return this.boundPorts.getFirstBinding();
9498
}

packages/testcontainers/src/test-container.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export interface TestContainer {
4747
withResourcesQuota(resourcesQuota: ResourcesQuota): this;
4848
withSharedMemorySize(bytes: number): this;
4949
withLogConsumer(logConsumer: (stream: Readable) => unknown): this;
50+
withHostname(hostname: string): this;
5051
}
5152

5253
export interface RestartOptions {
@@ -63,6 +64,7 @@ export interface StartedTestContainer {
6364
stop(options?: Partial<StopOptions>): Promise<StoppedTestContainer>;
6465
restart(options?: Partial<RestartOptions>): Promise<void>;
6566
getHost(): string;
67+
getHostname(): string;
6668
getFirstMappedPort(): number;
6769
getMappedPort(port: number): number;
6870
getName(): string;

0 commit comments

Comments
 (0)