Skip to content

Commit df3fe20

Browse files
fix: update backend IP handling to use published host port
1 parent 2fa9cda commit df3fe20

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

utils/duckietown_viewer_utils.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -208,12 +208,14 @@ def __init__(self, verbose: bool = False):
208208
# internal state
209209
self._backend: Optional[Container] = None
210210
self._frontend: Optional[subprocess.Popen] = None
211-
self._backend_ip: Optional[str] = None
211+
self._backend_url: Optional[str] = None
212212

213213
def start(self, app: str, robot: Optional[str], fullscreen: Optional[bool], menu: Optional[bool], on_top: Optional[bool], enable_hardware_acceleration: Optional[bool], browser: bool = False, window_args: Optional[WindowArgs] = None):
214214
if "url" not in window_args.keys():
215215
self._start_backend(app, robot)
216-
self._wait_backend_ready()
216+
if not self._wait_backend_ready():
217+
self._backend.stop()
218+
return
217219
if browser:
218220
url = f"http://localhost:{self._host_port}/app/"
219221
if not webbrowser.open(url):
@@ -303,17 +305,18 @@ def _wait_backend_ready(self) -> bool:
303305
container_name: str = container.name
304306
dtslogger.debug(f"Waiting for container '{container_name}' to be ready...")
305307

306-
# retrieve container's IP address and port
308+
# retrieve container's published port on the host
307309
container.reload()
308-
container_ip: str = container.network_settings.ip_address
309310
self._host_port: str = container.network_settings.ports[f"{self._BACKEND_REMOTE_PORT}/tcp"][0]["HostPort"]
310311

311-
dtslogger.debug(f"Container '{container_name}' is reachable at the IP address '{container_ip}'")
312+
# use localhost with the published host port (more reliable across Docker versions)
313+
backend_url = f"localhost:{self._host_port}"
314+
dtslogger.debug(f"Container '{container_name}' is reachable at '{backend_url}'")
312315
# wait for the backend to be ready
313316
stime: float = time.time()
314317
timeout: float = 10
315318
while True:
316-
url: str = f"http://{container_ip}:{self._BACKEND_REMOTE_PORT}/"
319+
url: str = f"http://{backend_url}/"
317320
try:
318321
response = requests.get(url)
319322
dtslogger.debug(f"GET: {url}\n < {response.status_code} {response.reason}")
@@ -325,7 +328,7 @@ def _wait_backend_ready(self) -> bool:
325328
# ready
326329
if response.status_code == 200:
327330
dtslogger.debug(f"Container '{container_name}' is ready")
328-
self._backend_ip = container_ip
331+
self._backend_url = backend_url
329332
return True
330333
# timeout
331334
if time.time() - stime > timeout:
@@ -337,11 +340,9 @@ def _wait_backend_ready(self) -> bool:
337340
def _start_frontend(self, fullscreen: Optional[bool], menu: Optional[bool], on_top: Optional[bool], enable_hardware_acceleration: Optional[bool], args: WindowArgs):
338341
app_config = ["--no-sandbox"]
339342
if "url" not in args.keys():
340-
if self._backend_ip is None:
343+
if self._backend_url is None:
341344
raise ValueError("Backend not ready. This should not have happened.")
342-
app_config.extend([
343-
"--url",
344-
f"http://{self._backend_ip}:{self._BACKEND_REMOTE_PORT}/app/"])
345+
app_config.extend(["--url", f"http://{self._backend_url}/app/"])
345346
if fullscreen:
346347
app_config.append("--fullscreen")
347348
if menu:

0 commit comments

Comments
 (0)