How to Add a Web UI to a Running Containerized Agent

    This article shows how to attach a browser UI to an existing containerized agent without rebuilding the image, migrating data, or adding a latency-heavy HTTP middle tier. It covers in-process Python calls, volume-mounted UI code, a combined startup command, SSE-aware reverse proxy settings, security hardening, common pitfalls, and a verification checklist.

    Background: Why Add a Web UI to an Existing Container

    Many teams first ship an agent as a container and interact with it through a terminal or internal scripts. As usage grows, they often want a browser UI for conversation debugging, task observation, and controlled collaboration. The natural requirement is simple: do not rebuild the image, do not migrate data, and do not insert a middle service that slows responses.

    This article describes a general practice: inject the Web UI by mounting a host directory into the existing container, start it as a child process inside the container, and reuse the agent’s Python modules and runtime context. The result is a zero-rebuild UI add-on.

    Architecture: In-Process Calls vs. an HTTP API Layer

    There are two common ways to add a UI to a containerized agent.

    Path 1: Deploy a separate web service and call the agent over HTTP. This is fully decoupled, but every request follows a chain of web service, serialization, HTTP transport, parsing inside the agent process, and a return trip. For streaming output, long context, and tool-call-heavy workloads, that extra hop increases latency and requires a separate API contract to maintain.

    Path 2: Have the Web UI import the agent’s Python modules and call them in the same process, or in a process group inside the same container. The UI and the agent core talk through function calls, with no network serialization cost, so responsiveness matches the terminal. The UI can also reach internal capabilities such as memory, skills, and scheduled tasks, instead of being limited to whatever a generic HTTP API exposes.

    When the goal is to add a UI quickly to an existing container, path 2 is the better fit. The Web UI becomes an add-on inside the container, not a standalone service.

    Orchestration Change: One Config Completes the Add-On

    If the container is managed by an orchestration file, the simplest approach is to put UI startup into the service definition. Replace the service name and paths to match your setup:

    1. services:
    2. agent:
    3. image: your-agent-image:latest
    4. container_name: agent
    5. restart: unless-stopped
    6. command: >
    7. /bin/sh -c "/opt/webui/start.sh & agent gateway run"
    8. ports:
    9. - "127.0.0.1:8787:8787"
    10. volumes:
    11. - ~/.agent/data:/opt/data
    12. - ~/.agent/webui:/opt/webui
    13. environment:
    14. - HOME=/opt/data/home
    15. - TZ=Asia/Shanghai
    16. - WEBUI_AGENT_DIR=/opt/agent
    17. - WEBUI_HOST=0.0.0.0
    18. - WEBUI_PASSWORD=replace-with-a-strong-password

    Key points:

    1. Inject the Web UI by mount; do not bake it into the image. Place the UI code on the host and mount it to a fixed path inside the container. The UI can be upgraded on its own, the agent image stays unchanged, and the existing container does not need to be rebuilt.
    2. Use a combined start command. Run the UI script in the background and keep the original gateway or main process in the foreground. The container’s main process must stay in the foreground; otherwise the orchestrator treats the container as exited and restarts it in a loop.
    3. Point at the agent source directory. An environment variable tells the Web UI where to import agent modules, so the UI uses the same code the container is already running and does not silently attach to another version.
    4. Bind the listen address carefully. The service inside the container must listen on 0.0.0.0 for port mapping to work. On the host, map only to 127.0.0.1 so the port is not exposed to the public internet by default.
    5. Persist the data directory. Memory, configuration, and other state must live on a mounted volume so a container rebuild does not drop them.

    After applying the config with up -d, open http://localhost:8787 in a browser. Because this is a hot-load style change, existing agent sessions and data do not need to be migrated.

    Remote Access: The Reverse Proxy Must Handle Streaming

    If the service runs on a VPS, put a reverse proxy in front of it. A common failure is streaming. The Web UI usually streams with SSE (Server-Sent Events), and proxies buffer responses by default. That makes the UI appear stuck, dump text in large bursts, or time out. Disable buffering and caching explicitly, and relax the read timeout:

    1. location / {
    2. proxy_pass http://127.0.0.1:8787;
    3. proxy_buffering off;
    4. proxy_cache off;
    5. proxy_read_timeout 86400s;
    6. }

    Also:

    • Serve external traffic over HTTPS so credentials are not sent in the clear.
    • Set sensible timeouts for long-lived connections so the proxy does not cut off long agent tasks.
    • For stricter access control, add proxy-layer authentication or an IP allowlist on top of the application password.

    Security Hardening Before Any Public Exposure

    1. Require a UI password. If the port might be reachable from outside, set WEBUI_PASSWORD to a strong password. Binding to loopback is not enough by itself: other processes on the same host can still reach that port.
    2. Do not publish the container port directly as -p 0.0.0.0:8787:8787. Terminate external access at the reverse proxy.
    3. Minimize mount scope. Mount only the directories the UI needs. Do not map sensitive host paths into the container.
    4. Review container privileges. After the UI is added, the attack surface is the agent process plus a web service. Where needed, run as a non-root user, use a read-only root filesystem, and drop capabilities.

    Three Common Pitfalls and How to Avoid Them

    Pitfall 1: The start script runs under the wrong interpreter. If start.sh is not executable or does not declare an interpreter, the combined command can fail silently and the UI port never comes up. Make the script executable, set the interpreter correctly, and invoke it explicitly through a shell in the orchestrated start command. To debug, enter the container and run the script by hand to see the error.

    Pitfall 2: Processes fight over the foreground. Two foreground processes cannot share one command. Keep the agent main process in the foreground and the UI in the background. Redirect UI logs to a file or to standard output; otherwise container logs interleave and are hard to read.

    Pitfall 3: Reverse-proxy buffering breaks streaming. As noted above, any proxied SSE response must have buffering disabled. If the UI works locally but remote output arrives in slides, check proxy buffering first rather than blaming agent performance.

    Verification Checklist

    After the change, confirm the following in order:

    • The container is running and is not in a restart loop.
    • http://localhost:8787 opens a login page locally.
    • After login, a streaming conversation produces output incrementally.
    • Agent memory, skills, scheduled tasks, and similar features can be triggered from the UI.
    • Data and sessions remain after a container restart.
    • Streaming stays smooth when accessed remotely through the reverse proxy.
    • The port is not exposed directly to the public internet, and the password is in effect.

    Summary

    Adding a Web UI to an existing containerized agent comes down to code injection plus in-process calls: mount the UI code into the container, coexist with the main process through a combined start command, and point an environment variable at the agent module path. Compared with standing up a separate HTTP API layer, the change is smaller, adds no extra network hop, and keeps the agent’s native capabilities. With the right reverse-proxy settings and security hardening, a terminal-only tool can become a collaborative web service in a short amount of time.