Adding a Web UI to an Existing AI Agent Container: A Practical Guide
This guide explains how to add a web interface to an already-running AI agent container without rebuilding the image. It compares in-process module import with a standalone HTTP bridge, then walks through the in-process approach with a Docker Compose example, key environment variables, local-only port binding, verification steps, and operational details such as persistence, log separation, concurrency, rollback, and observability.
1. The Problem: The Container Is Already Running, So Where Does the UI Go?
Many teams hit the same request after containerizing an AI agent service: terminal interaction works fine, but team members prefer a browser and want a visual interface to review sessions, invoke skills, and manage scheduled tasks. The catch is that the container is already running, and rebuilding the image means revalidating dependencies and remigrating data, which is not cheap. A more pragmatic approach is to leave the existing container and data volumes untouched and attach the UI layer incrementally.
Before starting, one decision has to be made: should the UI layer talk to the agent core through in-process calls or through cross-process HTTP calls? That choice directly determines response latency, feature completeness, and operational complexity down the line.
2. Trade-offs Between the Two Approaches
Option 1: In-Process Import (Low Overhead, Full Feature Parity)
The UI service imports the agent core as a Python module and handles session dispatch and result return inside the same process. With no network serialization or deserialization step, response behavior matches terminal interaction almost exactly. Because the UI holds the same objects and data structures, distinctive capabilities such as skills, memory, and cron jobs are recognized natively, with no extra adapter layer.
The cost is tighter coupling: the UI process and the core process share a lifecycle, so a failure on either side affects the other. Process supervision and automatic restart policies are therefore mandatory.
Option 2: Standalone Service with an HTTP Bridge (Decoupled, More Generic)
The UI runs as a separate service and communicates with the agent gateway over HTTP. The upside is a clean boundary and independent scaling. The downside is a network round trip on every interaction, and generic UIs often have limited support for proprietary capabilities, which leads to a split experience where the UI can chat but cannot show scheduled tasks or memory state.
Bottom line: if the goal is an experience identical to the terminal with a one-time setup, choose Option 1 first. If the team already has a unified gateway and authentication system, consider Option 2.
3. Implementation Steps (Using Option 1)
Step 1: Prepare the UI Code Directory
Clone the UI project to a fixed path on the host, for example ~/.hermes/webui, and mount it into the container as a data volume. Upgrading the UI then only requires updating the directory on the host and restarting the container, with no image rebuild.
Step 2: Orchestrate the Startup Command
The key is to keep both processes alive: run the UI process in the background and the gateway process in the foreground so the container’s main process does not exit. An illustrative configuration:
services:agent:image: your-agent-image:latestcontainer_name: agentrestart: unless-stoppedcommand: >bash -c "bash /opt/webui/start.sh & agent gateway run"ports:- "127.0.0.1:8787:8787"volumes:- ~/.hermes/data:/opt/data- ~/.hermes/webui:/opt/webuienvironment:- HOME=/opt/data/home- TZ=Asia/Shanghai- WEBUI_AGENT_DIR=/opt/agent- WEBUI_HOST=0.0.0.0- WEBUI_PASSWORD=replace-with-a-strong-password
Three variables need explanation. WEBUI_AGENT_DIR points to the core source directory inside the container and is what the UI uses to import the modules. WEBUI_HOST must be set to 0.0.0.0 for the UI to be reachable from outside the container. WEBUI_PASSWORD is the only line of defense at the entry point and must be a high-strength password.
Step 3: Bind the Port to Localhost Only
Writing the host port as 127.0.0.1 means only the local machine can reach it; remote access has to go through a jump host or a reverse proxy. This is the cheapest security boundary available and keeps the UI from being exposed directly to the public internet.
8787
Step 4: Start and Verify
After running up -d with your orchestration tool, check the container logs to confirm the UI process is listening, then open the local port to verify login and one complete conversation. If the logs show a module import failure, the cause is usually a wrong WEBUI_AGENT_DIR path or a missing dependency, not a network problem.
4. Operational Details That Are Easy to Miss
- Persistence: session records, the memory store, and scheduled task definitions must all live on mounted volumes, otherwise a container rebuild loses the data.
- Log separation: the UI process and the gateway process share the same stdout, so redirecting the UI to its own log file noticeably improves troubleshooting speed.
- Resources and concurrency: with the in-process approach, long-running tasks occupy the same event loop, so limit the number of concurrent sessions when necessary to avoid UI stalls being mistaken for failures.
- Upgrade and rollback: back up the data volume first, then update the UI directory, and keep the previous version directory for second-level rollback.
- Observability: connect a general-purpose logging service and monitoring alerts, with thresholds on container liveness, port availability, and error rate, which is more reliable than manual inspection.
5. Additional Notes on Cloud Deployment
If you move this structure to the cloud, container orchestration can be handed to a container platform, data volumes can be backed by cloud disks or object storage for backup, the entry layer can be consolidated behind a load balancer with unified authentication, and logs and metrics can go to a logging service and monitoring alerts respectively. Baidu AI Cloud typically offers these general-purpose capability categories for such scenarios, and they can be combined as needed without changing application-layer code.
6. Summary
Adding a web UI to an existing agent container is fundamentally an engineering exercise in process orchestration, data volumes, and a minimal security boundary. Pick the right call path, get the startup order right, tighten the port and the password, and most scenarios will work on the first attempt without changing how the terminal is used.
