Overview
SmolVM providesexpose_local() to forward TCP ports from the guest VM to localhost on your host machine. This allows you to access services running inside the VM (like web servers, databases, or APIs) from your host.
Basic Port Forwarding
Expose a guest port to localhost:Automatic Port Allocation
Omithost_port to let SmolVM choose an available port:
Method Signature
Transport Methods
SmolVM automatically selects the best transport method:- nftables (Preferred)
- SSH Tunnel (Fallback)
Uses Linux nftables for high-performance port forwarding:Advantages:
- Native Linux networking
- Higher performance
- Lower overhead
You don’t need to choose the transport method manually. SmolVM tries nftables first and falls back to SSH tunnels automatically.
Multiple Port Forwards
Expose multiple services from the same VM:Removing Port Forwards
Manually remove a port forward before VM shutdown:Method Signature
Port forwards are automatically cleaned up when the VM stops or the context manager exits. Manual cleanup is only needed for long-running VMs.
Real-world Example: OpenClaw Dashboard
From the OpenClaw integration example:Security Considerations
Localhost-only Binding
Port Conflicts
expose_local() handles port conflicts gracefully:
Troubleshooting
Port Forward Not Reachable
Ifexpose_local() succeeds but the port is not reachable:
1
Verify the service is running
2
Check the service binds to the correct interface
Services must bind to
0.0.0.0 or the guest IP, NOT 127.0.0.1 only:3
Test connectivity from inside the guest
Permission Errors (nftables)
If you see permission errors related to nftables:Next Steps
Environment Variables
Inject configuration into VMs at runtime
AI Agent Integration
Build secure sandboxes for AI agents
Custom Images
Create custom VM images with pre-installed services