(dcli *docker.Client, dc dnode, privatePort string)
| 391 | } |
| 392 | |
| 393 | func publicPort(dcli *docker.Client, dc dnode, privatePort string) (string, error) { |
| 394 | // TODO(aman): we should cache the port information |
| 395 | |
| 396 | if runtime.GOOS == "darwin" { |
| 397 | return getPortMappingsOnMac(dc.cid(), privatePort) |
| 398 | } |
| 399 | |
| 400 | ctx, cancel := context.WithTimeout(context.Background(), requestTimeout) |
| 401 | defer cancel() |
| 402 | |
| 403 | info, err := dcli.ContainerInspect(ctx, dc.cid()) |
| 404 | if err != nil { |
| 405 | return "", errors.Wrap(err, "error inspecting container") |
| 406 | } |
| 407 | |
| 408 | for port, bindings := range info.NetworkSettings.Ports { |
| 409 | if len(bindings) == 0 { |
| 410 | continue |
| 411 | } |
| 412 | if port.Port() != privatePort { |
| 413 | continue |
| 414 | } |
| 415 | |
| 416 | for _, binding := range bindings { |
| 417 | if binding.HostIP == "0.0.0.0" { |
| 418 | return binding.HostPort, nil |
| 419 | } |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | return "", fmt.Errorf("no mapping found for private port [%v] for container [%v]", privatePort, dc.cname()) |
| 424 | } |
| 425 | |
| 426 | // getPortMappingsOnMac parses `docker ps` output to get accurate macOS mappings |
| 427 | // because docker inspect does not know about port mappings at all. |
no test coverage detected