1414"""Module for LocalDockerModelServerRunner."""
1515
1616import os
17+ import subprocess
1718import time
18- from typing import Any , Dict , Optional
19+ from typing import Any , Dict , Optional , Tuple
1920
2021from absl import logging
2122import docker
@@ -39,7 +40,7 @@ def _make_docker_client(config: infra_validator_pb2.LocalDockerConfig):
3940 return docker .DockerClient (** params )
4041
4142
42- def _find_host_port (ports : Dict [str , Any ], container_port : int ) -> str :
43+ def _find_host (ports : Dict [str , Any ], container_port : int ) -> Tuple [ str , str ] :
4344 """Find host port from container port mappings.
4445
4546 `ports` is a nested dictionary of the following structure:
@@ -65,10 +66,15 @@ def _find_host_port(ports: Dict[str, Any], container_port: int) -> str:
6566 Raises:
6667 ValueError: No corresponding host port was found.
6768 """
69+ command = '/sbin/ip route|awk \' /default/ { print $3 }\' '
70+ ip_address = subprocess .check_output (
71+ command , shell = True ).decode ('utf-8' ).strip ()
72+ logging .info ('Container IP address: %s' , ip_address )
73+
6874 mappings = ports .get ('{}/tcp' .format (container_port ), [])
6975 for mapping in mappings :
7076 if mapping ['HostIp' ] == '0.0.0.0' :
71- return mapping ['HostPort' ]
77+ return ip_address , mapping ['HostPort' ]
7278 else :
7379 raise ValueError (
7480 'No HostPort found for ContainerPort={} (all port mappings: {})'
@@ -146,9 +152,11 @@ def WaitUntilRunning(self, deadline):
146152 continue
147153 # The container is running :)
148154 if status == 'running' :
149- host_port = _find_host_port (self ._container .ports ,
150- self ._serving_binary .container_port )
151- self ._endpoint = 'localhost:{}' .format (host_port )
155+ ip_address , host_port = _find_host (
156+ self ._container .ports ,
157+ self ._serving_binary .container_port )
158+
159+ self ._endpoint = '{}:{}' .format (ip_address , host_port )
152160 return
153161 # Docker status is one of {'created', 'restarting', 'running', 'removing',
154162 # 'paused', 'exited', or 'dead'}. Status other than 'created' and
0 commit comments