OpenHands icon indicating copy to clipboard operation
OpenHands copied to clipboard

Add port mappings support

Open amanape opened this issue 1 year ago • 5 comments

This PR adds support for port mappings

Backend

  • Conditionally extend instructions given Runtime instance
  • Add support for custom port mappings from config
  • Add port_mappings field to SandboxConfig

Users can add custom port mappings in their config like this:

config = AppConfig(
    sandbox=SandboxConfig(
        port_mappings={
            8080: 8080,  # map container port 8080 to host port 8080
            5000: 5001,  # map container port 5000 to host port 5001
        }
    )
)

Frontend

image

image
  • Create a new tab to display forwarded content
  • Query localhost:4141 in intervals to conditionally render online/offline text (refetch interval is set to 3000ms)

Example (Vite + React frontend)

  1. Start new chat
  2. [In Terminal] yes | npm create vite my-react-app -- --template react (this creates a Vite + React app in my-react-app)
  3. Wait to complete
  4. [In Terminal] cd my-react-app && npm install
  5. Wait to complete (may take a few seconds)
  6. Edit vite.config.js to
    import { defineConfig } from 'vite'
    import react from '@vitejs/plugin-react'
    
    // https://vite.dev/config/
    export default defineConfig({
      plugins: [react()],
      server: {
        host: "0.0.0.0", // Important to expose port outside the container
        port: 4141, // Target port
      },
    })
    
  7. Run npm run dev or ask OpenHands to run the server
  8. You should be able to see the same page as in the screenshot above
image

To run this PR locally, use the following command:

docker run -it --rm   -p 3000:3000   -v /var/run/docker.sock:/var/run/docker.sock   --add-host host.docker.internal:host-gateway   -e SANDBOX_RUNTIME_CONTAINER_IMAGE=docker.all-hands.dev/all-hands-ai/runtime:1854883-nikolaik   --name openhands-app-1854883   docker.all-hands.dev/all-hands-ai/openhands:1854883

amanape avatar Dec 13 '24 06:12 amanape

I have extended the remote and base runtime classes to include port mapping as well as extend the conversation route that the FE now depends on to retrieve the potentially available ports. Might be missing something though, would love some insight @xingyaoww @rbren

amanape avatar Dec 17 '24 08:12 amanape

This is looking good!

rbren avatar Dec 20 '24 16:12 rbren

Could we prioritize merging this PR? The current code restricts port export from runtime containers, and this PR resolves that issue. I tested a react app over 4141 port and it worked. I can help further in testing if needed.

kvchitrapu avatar Dec 27 '24 18:12 kvchitrapu

FYI I just pushed a change here. We can't hard-code the port mapping, because in that case you can't start multiple containers simultaneously. Instead we now look for available ports dynamically (same as w/ vscode)

rbren avatar Jan 01 '25 19:01 rbren

This is great! Looking for this feature!

carnalim avatar Jan 04 '25 17:01 carnalim