Skip to content

feat: add host network support for V1 DockerSandboxService#12445

Merged
tofarr merged 13 commits intoOpenHands:mainfrom
MkDev11:feat/docker-sandbox-host-network
Jan 28, 2026
Merged

feat: add host network support for V1 DockerSandboxService#12445
tofarr merged 13 commits intoOpenHands:mainfrom
MkDev11:feat/docker-sandbox-host-network

Conversation

@MkDev11
Copy link
Copy Markdown
Contributor

@MkDev11 MkDev11 commented Jan 15, 2026

Summary of PR

This PR adds host network support to V1's DockerSandboxService, addressing the issue where user-launched applications on non-standard ports (e.g., Flask on port 5000) are inaccessible via reverse proxy setups.

Changes:

  • Add use_host_network field to DockerSandboxService and DockerSandboxServiceInjector
  • When enabled, containers use host network mode (network_mode='host')
  • Container ports are directly accessible on the host without dynamic port mapping
  • Update _container_to_sandbox_info to handle host network mode properly
  • Configurable via OH_SANDBOX_USE_HOST_NETWORK environment variable

How it works:

  • Bridge mode (default): Container ports are mapped to random host ports (existing behavior)
  • Host mode: Container shares host's network namespace, all ports directly accessible

This allows reverse proxy setups (nginx/CloudFront) using patterns like /runtime/{port}/localhost:{port} to work correctly with any port, not just the 4 hardcoded ports (8000, 8001, 8011, 8012).

Demo Screenshots/Videos

N/A - Backend infrastructure change, no UI changes.

Change Type

  • Bug fix
  • New feature
  • Breaking change
  • Refactor
  • Other (dependency update, docs, typo fixes, etc.)

Checklist

  • I have read and reviewed the code and I understand what the code is doing.
  • I have tested the code to the best of my ability and ensured it works as expected.

Fixes

Resolves #12403

Release Notes

  • Include this change in the Release Notes.

V1 DockerSandboxService now supports host networking mode via OH_SANDBOX_USE_HOST_NETWORK=true. This enables reverse proxy setups to access user-launched applications on any port, not just the predefined worker ports.

- Add use_host_network field to DockerSandboxService and DockerSandboxServiceInjector
- When enabled, containers use host network mode (network_mode='host')
- Container ports are directly accessible on the host without port mapping
- Update _container_to_sandbox_info to handle host network mode
- Configurable via OH_SANDBOX_USE_HOST_NETWORK environment variable

This allows reverse proxy setups (nginx/CloudFront) to work correctly
with user-launched applications on any port, not just the 4 hardcoded ports.

Fixes OpenHands#12403
@MkDev11
Copy link
Copy Markdown
Contributor Author

MkDev11 commented Jan 23, 2026

Hello @hieptl @tofarr could you please review the implementation and let me know your feedbacks? thanks

Comment thread openhands/app_server/sandbox/docker_sandbox_service.py Outdated
Comment thread openhands/app_server/sandbox/docker_sandbox_service.py
Comment thread openhands/app_server/sandbox/docker_sandbox_service.py
MkDev11 and others added 2 commits January 26, 2026 15:38
- Fix type inconsistency: convert host_port to int in bridge mode (line 174)
- Add warning for port collision risk when use_host_network=True with max_num_sandboxes > 1
- Add logging when starting sandbox with host network mode
@MkDev11
Copy link
Copy Markdown
Contributor Author

MkDev11 commented Jan 26, 2026

@hieptl just fixed the issues, can you please review it again?

Comment thread openhands/app_server/sandbox/docker_sandbox_service.py
Comment thread openhands/app_server/sandbox/docker_sandbox_service.py
…path

Address PR review comment: The Field description mentioned configuring via
environment variable, but config.py didn't parse SANDBOX_USE_HOST_NETWORK
in the legacy fallback path like other sandbox env vars.
…k default

- Test warning log when use_host_network=True and max_num_sandboxes > 1
- Test no warning when max_num_sandboxes=1
- Test DockerSandboxServiceInjector use_host_network defaults to False
- Test DockerSandboxServiceInjector use_host_network can be enabled
Comment thread openhands/app_server/sandbox/docker_sandbox_service.py Outdated
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.

Great work overall. My only suggestion is to keep the docker specific config inside the docker_sandbox_service.

Thank you for taking the time to fix this issue.

Move the SANDBOX_USE_HOST_NETWORK environment variable handling from
config.py to the DockerSandboxServiceInjector field default, as
suggested in PR review. This ensures the legacy env var fallback is
handled at the field definition level.
@MkDev11
Copy link
Copy Markdown
Contributor Author

MkDev11 commented Jan 28, 2026

@tofarr thanks for your feedback! please review the changes again

@MkDev11 MkDev11 requested a review from tofarr January 28, 2026 10:57
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.

Great work!

@tofarr tofarr merged commit c483c80 into OpenHands:main Jan 28, 2026
16 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.

[Feature Request] V1 DockerSandboxService: Support for host networking and additional exposed ports

3 participants