Skip to content

Fix WebSocket localhost bug by passing DOCKER_HOST_ADDR to runtime containers#12113

Merged
tofarr merged 1 commit intoOpenHands:mainfrom
shanemort1982:fix-websocket-localhost-docker-host-addr
Dec 24, 2025
Merged

Fix WebSocket localhost bug by passing DOCKER_HOST_ADDR to runtime containers#12113
tofarr merged 1 commit intoOpenHands:mainfrom
shanemort1982:fix-websocket-localhost-docker-host-addr

Conversation

@shanemort1982
Copy link
Copy Markdown
Contributor

@shanemort1982 shanemort1982 commented Dec 20, 2025

Description

Fixes WebSocket connection failures when accessing OpenHands from a remote browser.

Problem

When accessing OpenHands from a remote machine, WebSocket connections fail with:

Firefox can't establish a connection to the server at ws://localhost:42295/sockets/events/...

The browser is on a different machine than the server, so localhost is incorrect.

Root Cause

The DOCKER_HOST_ADDR environment variable is not passed from the main OpenHands container to spawned runtime and agent-server containers, causing them to default to localhost for URL construction.

Solution

This PR adds code to pass DOCKER_HOST_ADDR to runtime and agent-server containers, allowing existing code in sandbox_config.py, docker_runtime.py, and config.py to correctly use the server IP address for WebSocket and API URLs.

Changes

  1. File: openhands/runtime/impl/docker/docker_runtime.py

    • Lines: 475-477
    • Change: Pass DOCKER_HOST_ADDR from main container to runtime containers
  2. File: openhands/app_server/sandbox/docker_sandbox_service.py

    • Lines: 313-315
    • Change: Pass DOCKER_HOST_ADDR from main container to agent-server containers

Testing ✅

Deployment Test (OpenHands 1.0):

  • Server: 192.168.1.206
  • Client: Remote mobile browser (192.168.1.175)
  • Command: docker run -e DOCKER_HOST_ADDR=192.168.1.206 -p 3000:3000 ...

Results:

✅ Agent-server containers receive DOCKER_HOST_ADDR=192.168.1.206

✅ All generated URLs use 192.168.1.206 instead of localhost:

http://192.168.1.206:42015/api/conversations/...
http://192.168.1.206:52051/... (VSCode)
http://192.168.1.206:57233/... (worker)
http://192.168.1.206:39157/... (worker)

✅ Remote browser successfully connects to correct IP addresses

✅ No Python import errors

✅ Container starts and runs normally

Before this fix: All URLs showed localhost, causing connection failures from remote browsers

After this fix: All URLs show the actual server IP, enabling remote access

Related Issues

Environment Variable Flow

Host System (-e DOCKER_HOST_ADDR=<ip>)
    ↓
OpenHands-app Container
    ↓ [THIS FIX]
Runtime & Agent-Server Containers
    ↓
URLs use server IP instead of localhost

Backward Compatibility

  • If DOCKER_HOST_ADDR is not set, behavior is unchanged (defaults to localhost)
  • No breaking changes
  • Works with local and remote deployments

Note

A separate CORS configuration issue was discovered during testing (unrelated to this fix) where agent-server containers need CORS headers to allow cross-origin requests from the main app. This will be addressed in a separate PR.

…ntainers

When accessing OpenHands from a remote browser, WebSocket connections fail
because URLs use 'localhost' instead of the actual server IP address.

Root cause: The DOCKER_HOST_ADDR environment variable is not passed from
the main OpenHands container to spawned runtime containers, causing them
to default to localhost for URL construction.

This fix ensures that DOCKER_HOST_ADDR is inherited by runtime containers,
allowing existing code in sandbox_config.py and docker_runtime.py to
correctly use the server IP for WebSocket and API URLs.

Fixes remote access scenarios where browser and server are on different machines.

Related to OpenHands#8423 (previous fix for old architecture)
@shanemort1982 shanemort1982 force-pushed the fix-websocket-localhost-docker-host-addr branch from e984cf5 to 37cc14d Compare December 22, 2025 07:42
Copy link
Copy Markdown
Collaborator

@tofarr tofarr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM - though we are moving away from this class in V1.

@tofarr tofarr merged commit 5407ea5 into OpenHands:main Dec 24, 2025
22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants