Skip to content

Commit b7510a9

Browse files
committed
don't have to do hostconfig mapping if re-inspecting after re-starting container
1 parent ca2ea4e commit b7510a9

File tree

2 files changed

+6
-28
lines changed

2 files changed

+6
-28
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,15 @@ export class GenericContainer implements TestContainer {
128128
}
129129

130130
private async reuseContainer(client: ContainerRuntimeClient, container: Container) {
131-
const inspectResult = await client.container.inspect(container);
132-
const mappedInspectResult = mapInspectResult(inspectResult);
133-
if (!mappedInspectResult.state.running) {
131+
let inspectResult = await client.container.inspect(container);
132+
if (!inspectResult.State.Running) {
134133
log.debug("Reused container is not running, attempting to start it");
135134
await client.container.start(container);
135+
// Refetch the inspect result to get the updated state
136+
inspectResult = await client.container.inspect(container);
136137
}
137138

139+
const mappedInspectResult = mapInspectResult(inspectResult);
138140
const boundPorts = BoundPorts.fromInspectResult(client.info.containerRuntime.hostIps, mappedInspectResult).filter(
139141
this.exposedPorts
140142
);

packages/testcontainers/src/utils/map-inspect-result.ts

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@ export function mapInspectResult(inspectResult: ContainerInspectInfo): InspectRe
77
return {
88
name: inspectResult.Name,
99
hostname: inspectResult.Config.Hostname,
10-
ports: {
11-
...mapHostConfigPortBindings(inspectResult),
12-
...mapPorts(inspectResult),
13-
},
10+
ports: mapPorts(inspectResult),
1411
healthCheckStatus: mapHealthCheckStatus(inspectResult),
1512
networkSettings: mapNetworkSettings(inspectResult),
1613
state: {
@@ -23,27 +20,6 @@ export function mapInspectResult(inspectResult: ContainerInspectInfo): InspectRe
2320
};
2421
}
2522

26-
type HostConfigPortBindings = {
27-
[port: string]: Array<{
28-
HostIp: string;
29-
HostPort: string;
30-
}>;
31-
};
32-
33-
function mapHostConfigPortBindings(inspectInfo: ContainerInspectInfo): Ports {
34-
return Object.entries(inspectInfo.HostConfig.PortBindings as HostConfigPortBindings)
35-
.filter(([, hostPorts]) => hostPorts !== null)
36-
.map(([containerPort, hostPorts]) => {
37-
return {
38-
[parseInt(containerPort)]: hostPorts.map((hostPort) => ({
39-
hostIp: hostPort.HostIp,
40-
hostPort: parseInt(hostPort.HostPort),
41-
})),
42-
};
43-
})
44-
.reduce((acc, curr) => ({ ...acc, ...curr }), {});
45-
}
46-
4723
function mapPorts(inspectInfo: ContainerInspectInfo): Ports {
4824
return Object.entries(inspectInfo.NetworkSettings.Ports)
4925
.filter(([, hostPorts]) => hostPorts !== null)

0 commit comments

Comments
 (0)