Add port mappings support
This PR adds support for port mappings
Backend
- Conditionally extend instructions given
Runtimeinstance - Add support for custom port mappings from config
- Add
port_mappingsfield 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
- Create a new tab to display forwarded content
- Query
localhost:4141in intervals to conditionally render online/offline text (refetch interval is set to 3000ms)
Example (Vite + React frontend)
- Start new chat
- [In Terminal]
yes | npm create vite my-react-app -- --template react(this creates a Vite + React app inmy-react-app) - Wait to complete
- [In Terminal]
cd my-react-app && npm install - Wait to complete (may take a few seconds)
- Edit
vite.config.jstoimport { 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 }, }) - Run
npm run devor ask OpenHands to run the server - You should be able to see the same page as in the screenshot above
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
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
This is looking good!
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.
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)
This is great! Looking for this feature!