Fix WebSocket localhost bug by passing DOCKER_HOST_ADDR to runtime containers#12113
Merged
tofarr merged 1 commit intoOpenHands:mainfrom Dec 24, 2025
Merged
Conversation
…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)
e984cf5 to
37cc14d
Compare
tofarr
approved these changes
Dec 24, 2025
Collaborator
tofarr
left a comment
There was a problem hiding this comment.
LGTM - though we are moving away from this class in V1.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Fixes WebSocket connection failures when accessing OpenHands from a remote browser.
Problem
When accessing OpenHands from a remote machine, WebSocket connections fail with:
The browser is on a different machine than the server, so
localhostis incorrect.Root Cause
The
DOCKER_HOST_ADDRenvironment variable is not passed from the main OpenHands container to spawned runtime and agent-server containers, causing them to default tolocalhostfor URL construction.Solution
This PR adds code to pass
DOCKER_HOST_ADDRto runtime and agent-server containers, allowing existing code insandbox_config.py,docker_runtime.py, andconfig.pyto correctly use the server IP address for WebSocket and API URLs.Changes
File:
openhands/runtime/impl/docker/docker_runtime.pyDOCKER_HOST_ADDRfrom main container to runtime containersFile:
openhands/app_server/sandbox/docker_sandbox_service.pyDOCKER_HOST_ADDRfrom main container to agent-server containersTesting ✅
Deployment Test (OpenHands 1.0):
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.206instead oflocalhost:✅ 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 browsersAfter this fix: All URLs show the actual server IP, enabling remote access
Related Issues
Environment Variable Flow
Backward Compatibility
DOCKER_HOST_ADDRis not set, behavior is unchanged (defaults to localhost)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.