def get_main_endpoint_from_container(): global DOCKER_MAIN_CONTAINER_IP if not config.HOSTNAME_FROM_LAMBDA and DOCKER_MAIN_CONTAINER_IP is None: DOCKER_MAIN_CONTAINER_IP = False container_name = bootstrap.get_main_container_name() try: if config.is_in_docker: DOCKER_MAIN_CONTAINER_IP = DOCKER_CLIENT.get_container_ipv4_for_network( container_name_or_id=container_name, container_network=get_container_network_for_lambda(), ) else: # default gateway for the network should be the host # (only under Linux - otherwise fall back to DOCKER_HOST_FROM_CONTAINER below) if config.is_in_linux: DOCKER_MAIN_CONTAINER_IP = DOCKER_CLIENT.inspect_network( get_container_network_for_lambda( ))["IPAM"]["Config"][0]["Gateway"] LOG.info("Determined main container target IP: %s", DOCKER_MAIN_CONTAINER_IP) except Exception as e: LOG.info( 'Unable to get IP address of main Docker container "%s": %s', container_name, e) # return (1) predefined endpoint host, or (2) main container IP, or (3) Docker host (e.g., bridge IP) return (config.HOSTNAME_FROM_LAMBDA or DOCKER_MAIN_CONTAINER_IP or config.DOCKER_HOST_FROM_CONTAINER)
def get_endpoint_for_network(network: Optional[str] = None) -> str: """ Get the LocalStack endpoint (= IP address) on the given network. If a network is given, it will return the IP address/hostname of LocalStack on that network If omitted, it will return the IP address/hostname of the main container network This is a cached call, clear cache if networks might have changed :param network: Network to return the endpoint for :return: IP address or hostname of LS on the given network """ container_name = get_main_container_name() network = network or get_main_container_network() main_container_ip = None try: if config.is_in_docker: main_container_ip = DOCKER_CLIENT.get_container_ipv4_for_network( container_name_or_id=container_name, container_network=network, ) else: # default gateway for the network should be the host # (only under Linux - otherwise fall back to DOCKER_HOST_FROM_CONTAINER below) if config.is_in_linux: main_container_ip = DOCKER_CLIENT.inspect_network( network)["IPAM"]["Config"][0]["Gateway"] LOG.info("Determined main container target IP: %s", main_container_ip) except Exception as e: LOG.info('Unable to get IP address of main Docker container "%s": %s', container_name, e) # return (1) predefined endpoint host, or (2) main container IP, or (3) Docker host (e.g., bridge IP) return main_container_ip or config.DOCKER_HOST_FROM_CONTAINER