Repository: All-Hands-AI/OpenHands
File: openhands/app_server/sandbox/docker_sandbox_service.py (Python)

Code before:
            'Extra hostname mappings to add to agent-server containers. '
            'This allows containers to resolve hostnames like host.docker.internal '
            'for LAN deployments and MCP connections. '
            'Format: {"hostname": "ip_or_gateway"}'
        ),
    )
    startup_grace_seconds: int = Field(
        default=STARTUP_GRACE_SECONDS,
        description=(
            'Number of seconds were no response from the agent server is acceptable'
            'before it is considered an error'
        ),
    )
    use_host_network: bool = Field(
        default=False,
        description=(
            'Whether to use host networking mode for sandbox containers. '
            'When enabled, containers share the host network namespace, '
            'making all container ports directly accessible on the host. '
            'This is useful for reverse proxy setups where dynamic port mapping '
            'is problematic. Configure via OH_SANDBOX_USE_HOST_NETWORK environment variable.'
        ),
    )

    async def inject(
        self, state: InjectorState, request: Request | None = None
    ) -> AsyncGenerator[SandboxService, None]:
        # Define inline to prevent circular lookup
        from openhands.app_server.config import (
            get_global_config,
            get_httpx_client,
            get_sandbox_spec_service,
        )

        # Get web_url from global config for CORS support
        config = get_global_config()
        web_url = config.web_url

        async with (
            get_httpx_client(state) as httpx_client,
            get_sandbox_spec_service(state) as sandbox_spec_service,
        ):
            yield DockerSandboxService(
                sandbox_spec_service=sandbox_spec_service,
                container_name_prefix=self.container_name_prefix,
                host_port=self.host_port,
                container_url_pattern=self.container_url_pattern,
                mounts=self.mounts,
                exposed_ports=self.exposed_ports,
                health_check_path=self.health_check_path,
                httpx_client=httpx_client,

Review comment (suggestion):
**question:** Environment variable configuration may not work (docker_sandbox_service.py:590, config.py:199-235). The Field description says: "Configure via OH_SANDBOX_USE_HOST_NETWORK environment variable", but config.py doesn't  parse this environment variable in the legacy fallback path:                                                             
                                                                                                                           
```python
 # config.py lines 199-235 - legacy env vars are handled                                                                  
docker_sandbox_kwargs: dict = {}                                                                                         
if os.getenv('SANDBOX_HOST_PORT'):                                                                                       
    docker_sandbox_kwargs['host_port'] = int(os.environ['SANDBOX_HOST_PORT'])                                            
if os.getenv('SANDBOX_CONTAINER_URL_PATTERN'):                                                                           
    docker_sandbox_kwargs['container_url_pattern'] = os.environ['SANDBOX_CONTAINER_URL_PATTERN']                         
# ... but SANDBOX_USE_HOST_NETWORK is NOT handled!
```

Thank you! 🙏 

Code after:
            'Extra hostname mappings to add to agent-server containers. '
            'This allows containers to resolve hostnames like host.docker.internal '
            'for LAN deployments and MCP connections. '
            'Format: {"hostname": "ip_or_gateway"}'
        ),
    )
    startup_grace_seconds: int = Field(
        default=STARTUP_GRACE_SECONDS,
        description=(
            'Number of seconds were no response from the agent server is acceptable'
            'before it is considered an error'
        ),
    )
    use_host_network: bool = Field(
        default=os.getenv('SANDBOX_USE_HOST_NETWORK', '').lower()
        in (
            'true',
            '1',
            'yes',
        ),
        description=(
            'Whether to use host networking mode for sandbox containers. '
            'When enabled, containers share the host network namespace, '
            'making all container ports directly accessible on the host. '
            'This is useful for reverse proxy setups where dynamic port mapping '
            'is problematic. Configure via OH_SANDBOX_USE_HOST_NETWORK environment variable.'
        ),
    )

    async def inject(
        self, state: InjectorState, request: Request | None = None
    ) -> AsyncGenerator[SandboxService, None]:
        # Define inline to prevent circular lookup
        from openhands.app_server.config import (
            get_global_config,
            get_httpx_client,
            get_sandbox_spec_service,
        )

        # Get web_url from global config for CORS support
        config = get_global_config()
        web_url = config.web_url

        async with (
            get_httpx_client(state) as httpx_client,
            get_sandbox_spec_service(state) as sandbox_spec_service,
        ):
            yield DockerSandboxService(
                sandbox_spec_service=sandbox_spec_service,
                container_name_prefix=self.container_name_prefix,
                host_port=self.host_port,