Skip to content

Commit 0e4a798

Browse files
authored
Fix LocalStack container hostname resolution (#834)
1 parent f8f82a5 commit 0e4a798

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

packages/modules/localstack/src/localstack-container.test.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,5 +68,39 @@ describe("LocalStackContainer", () => {
6868
expect(response).toContain(`http://localstack:${LOCALSTACK_PORT}`);
6969
await container.stop();
7070
await awsCliInDockerNetwork.stop();
71+
await network.stop();
72+
});
73+
74+
it("should not override LOCALSTACK_HOST assignment", async () => {
75+
const container = await new LocalstackContainer()
76+
.withEnvironment({ LOCALSTACK_HOST: "myhost" })
77+
.withNetworkAliases("myalias")
78+
.start();
79+
80+
const { output, exitCode } = await container.exec(["printenv", "LOCALSTACK_HOST"]);
81+
expect(exitCode).toBe(0);
82+
expect(output).toContain("myhost");
83+
84+
await container.stop();
85+
});
86+
87+
it("should override LOCALSTACK_HOST with last network alias", async () => {
88+
const container = await new LocalstackContainer().withNetworkAliases("other", "myalias").start();
89+
90+
const { output, exitCode } = await container.exec(["printenv", "LOCALSTACK_HOST"]);
91+
expect(exitCode).toBe(0);
92+
expect(output).toContain("myalias");
93+
94+
await container.stop();
95+
});
96+
97+
it("should assign LOCALSTACK_HOST to localhost", async () => {
98+
const container = await new LocalstackContainer().start();
99+
100+
const { output, exitCode } = await container.exec(["printenv", "LOCALSTACK_HOST"]);
101+
expect(exitCode).toBe(0);
102+
expect(output).toContain("localhost");
103+
104+
await container.stop();
71105
});
72106
});

packages/modules/localstack/src/localstack-container.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ export const LOCALSTACK_PORT = 4566;
55
export class LocalstackContainer extends GenericContainer {
66
constructor(image = "localstack/localstack:2.2.0") {
77
super(image);
8-
this.resolveHostname();
98
this.withExposedPorts(LOCALSTACK_PORT).withWaitStrategy(Wait.forLogMessage("Ready", 1)).withStartupTimeout(120_000);
109
}
1110

@@ -16,13 +15,18 @@ export class LocalstackContainer extends GenericContainer {
1615
// do nothing
1716
hostnameExternalReason = "explicitly as environment variable";
1817
} else if (this.networkAliases && this.networkAliases.length > 0) {
19-
this.environment[envVar] = this.networkAliases.at(this.networkAliases.length - 1) || ""; // use the last network alias set
18+
// use the last network alias set
19+
this.withEnvironment({ [envVar]: this.networkAliases.at(this.networkAliases.length - 1) ?? "" });
2020
hostnameExternalReason = "to match last network alias on container with non-default network";
2121
} else {
22-
this.withEnvironment({ LOCALSTACK_HOST: "localhost" });
22+
this.withEnvironment({ [envVar]: "localhost" });
2323
hostnameExternalReason = "to match host-routable address for container";
2424
}
25-
log.info(`${envVar} environment variable set to ${this.environment[envVar]} (${hostnameExternalReason})"`);
25+
log.info(`${envVar} environment variable set to "${this.environment[envVar]}" (${hostnameExternalReason})`);
26+
}
27+
28+
protected override async beforeContainerCreated(): Promise<void> {
29+
this.resolveHostname();
2630
}
2731

2832
public override async start(): Promise<StartedLocalStackContainer> {

0 commit comments

Comments
 (0)