from typing import Dict
from typing import Any


def _find_host_port(ports: Dict[str, Any], container_port: int) -> str:
  """Find host port from container port mappings.

  `ports` is a nested dictionary of the following structure:

  {'8500/tcp': [{'HostIp': '0.0.0.0', 'HostPort': '32769'}],
   '8501/tcp': [{'HostIp': '0.0.0.0', 'HostPort': '32768'}]}

  Args:
    ports: Dictionary of docker container port mapping.
    container_port: Corresponding container port you're looking for.

  Returns:
    A found host port.

  Raises:
    ValueError: No corresponding host port was found.
  """
  mappings = ports.get('{}/tcp'.format(container_port))
  if mappings:
    return mappings[0].get('HostPort')
  else:
    raise ValueError(
        'No HostPort found for ContainerPort={} (all port mappings: {})'
        .format(container_port, ports))