Read the host port Docker mapped for `containerPort` on a running container.
( containerName: string, containerPort: number, out: DevOutput )
| 306 | |
| 307 | /** Read the host port Docker mapped for `containerPort` on a running container. */ |
| 308 | async function readMappedHostPort( |
| 309 | containerName: string, |
| 310 | containerPort: number, |
| 311 | out: DevOutput |
| 312 | ): Promise<number> { |
| 313 | const { stdout } = await runForwarded( |
| 314 | 'docker', |
| 315 | ['port', containerName, `${containerPort}/tcp`], |
| 316 | out, |
| 317 | { quiet: true } |
| 318 | ); |
| 319 | // Output like "0.0.0.0:54321" (possibly multiple lines for ipv4/ipv6). |
| 320 | const match = stdout.match(/:(\d+)\s*$/m); |
| 321 | if (!match) { |
| 322 | throw new Error( |
| 323 | `Could not determine mapped host port for ${containerName} ` + |
| 324 | `(${containerPort}/tcp). Got: ${stdout.trim()}` |
| 325 | ); |
| 326 | } |
| 327 | return Number(match[1]); |
| 328 | } |
| 329 | |
| 330 | function uniqueContainerName(serviceName: string): string { |
| 331 | const safe = serviceName.toLowerCase().replace(/[^a-z0-9-_.]/g, '-'); |
no test coverage detected