Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for specifying container hostname #889

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
12 changes: 12 additions & 0 deletions docs/features/containers.md
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,18 @@ const container = await new GenericContainer("alpine")
.start();
```

### With custom hostname

**Not recommended.**

See this [Docker blog post on Testcontainers best practices](https://www.docker.com/blog/testcontainers-best-practices/#:~:text=Don't%20hardcode%20the%20hostname)

```javascript
const container = await new GenericContainer("alpine")
.withHostname("my-hostname")
.start();
```

## Stopping a container

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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,16 @@ describe("GenericContainer", () => {
await secondStartedContainer.stop();
});

it("should set the hostname", async () => {
const container = await new GenericContainer("cristianrgreco/testcontainer:1.1.14")
.withHostname("hostname")
.start();

expect(container.getHostname()).toEqual("hostname");

await container.stop();
});

// failing to build an image hangs within the DockerImageClient.build method,
// that change might be larger so leave it out of this commit but skip the failing test
it.skip("should throw an error for a target stage that does not exist", async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -475,4 +475,9 @@ export class GenericContainer implements TestContainer {
this.logConsumer = logConsumer;
return this;
}

public withHostname(hostname: string): this {
this.createOpts.Hostname = hostname;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ export class StartedGenericContainer implements StartedTestContainer {
return this.host;
}

public getHostname(): string {
return this.inspectResult.Config.Hostname;
}

public getFirstMappedPort(): number {
return this.boundPorts.getFirstBinding();
}
Expand Down
2 changes: 2 additions & 0 deletions packages/testcontainers/src/test-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export interface TestContainer {
withResourcesQuota(resourcesQuota: ResourcesQuota): this;
withSharedMemorySize(bytes: number): this;
withLogConsumer(logConsumer: (stream: Readable) => unknown): this;
withHostname(hostname: string): this;
}

export interface RestartOptions {
Expand All @@ -63,6 +64,7 @@ export interface StartedTestContainer {
stop(options?: Partial<StopOptions>): Promise<StoppedTestContainer>;
restart(options?: Partial<RestartOptions>): Promise<void>;
getHost(): string;
getHostname(): string;
getFirstMappedPort(): number;
getMappedPort(port: number): number;
getName(): string;
Expand Down
Loading