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 containerized AI agent by running the UI as a thin in-process shell that imports the runtime modules directly, instead of placing a separate generic chat frontend in front of it. It covers the trade-offs between network-based and in-process integration, host directory and file planning, a startup script that keeps the gateway in the foreground, an orchestration configuration example, reverse proxy settings required for streaming output, a security baseline, and observability and troubleshooting tips for the shared container.

    Adding a Web UI to an Existing AI Agent Container: A Practical Guide

    1. Background: The Container Is Running, So How Do You Add the UI?

    When teams deploy an AI agent service, the first step is usually to containerize the core runtime: a long-running process that loads model configuration, registers Skills, maintains Memory, runs Cron jobs, and exposes everything through an internal gateway for unified scheduling. This architecture works well in a terminal, but the lack of a graphical interface becomes a problem as soon as non-technical colleagues need to use it, or you need to check task progress on mobile.

    The intuitive approach is to “spin up a generic chat UI and point it at the agent’s API.” It works, but it carries two hidden costs. First, it adds an extra network hop and protocol conversion, which degrades both latency and experience for streaming output. Second, a generic UI has no native support for agent-specific capabilities such as skill orchestration, memory management, and scheduled jobs, so many features have to be assembled manually.

    This article discusses a different path: make the web UI a thin in-process layer that imports the agent runtime’s modules and calls them directly, with no extra API middleware in between, so that both capability coverage and responsiveness stay as close to the terminal as possible.

    2. Choosing Between Two Integration Paths

    2.1 The Network API Approach

    The advantage is complete decoupling: the UI and the runtime can be upgraded and scaled independently. The drawbacks are equally clear:

    • An extra local or cross-machine network round trip increases time to first byte;
    • Streaming output needs additional protocol adaptation and is prone to buffering and truncation;
    • Runtime-specific internal capabilities (the skill registry, memory reads and writes, scheduled job state) can only be accessed indirectly, with incomplete coverage.

    2.2 The In-Process Direct Approach

    The UI service and the agent run in the same container, with the same file system and process space, and the UI process issues calls directly by importing the runtime modules. The key benefits are:

    • Zero network hops: calls complete inside the process, so the experience matches the terminal;
    • Zero capability loss: internal objects such as Skills, Memory, and Cron jobs can be read and written directly;
    • Simple deployment: no extra service discovery, auth gateway, or cross-container network policies.

    The cost is that the UI process and the runtime process share a lifecycle, so process orchestration is needed to make sure the whole stack is brought back up if either process exits unexpectedly. The concrete approach is given later in this article.

    3. Preparation Before Deployment

    3.1 Directory and File Planning

    It is best to keep UI-related files out of the agent source directory, place them in a dedicated directory on the host, and mount that directory into the container read-only or read-write. This way, upgrading the runtime image will not overwrite the UI files, and rollbacks are easy. A typical host layout looks like this:

    1. ~/.hermes/
    2. ├── data/ # Runtime mutable data, mapped user home directory
    3. └── webui/ # UI files and startup script
    4. ├── start.sh
    5. └── static/

    Inside the container, agree on two fixed paths: one pointing at the runtime source directory and one at the UI directory, both declared explicitly through environment variables so paths are not hard-coded in scripts.

    3.2 Writing the Startup Script

    The startup script does two things: it puts the UI process in the background and the runtime gateway in the foreground. The foreground process is the container’s main process and determines whether the container is “alive”; the background process serves the UI. Example:

    1. #!/usr/bin/env bash
    2. set -euo pipefail
    3. # Start the UI service in the background
    4. bash /opt/webui/start.sh &
    5. # Run the agent gateway in the foreground as the container's main process
    6. exec hermes gateway run

    Three details here are easy to get wrong: invoke the script explicitly with bash instead of relying on the executable bit; redirect the background task’s standard output to a log file or let the container log driver collect it; and replace the main process with exec so signals are delivered correctly and no orphan processes are left behind when the container stops.

    4. Orchestration Configuration Example

    If you already manage containers with an orchestration file, the easiest option is to write the UI startup directly into the service definition rather than creating a separate service. A core snippet:

    1. services:
    2. agent:
    3. image: your-agent-runtime:latest
    4. container_name: agent
    5. restart: unless-stopped
    6. command: >
    7. bash -c "bash /opt/webui/start.sh & exec agent-gateway run"
    8. ports:
    9. - "127.0.0.1:8787:8787"
    10. volumes:
    11. - ~/.hermes/data:/opt/data
    12. - ~/.hermes/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

    Four key points in this configuration are worth expanding on:

    1. Bind the port to the loopback address. Writing 127.0.0.1:8787:8787 means only the host machine can reach it and the public network interface is not exposed directly. When external access is needed, put a reverse proxy in front of it instead of opening the container port to the world.
    2. Pass the runtime directory in through an environment variable. The UI process uses it to locate the modules, so when an image upgrade changes the path, you only change the environment variable.
    3. Mount the data directory and the UI directory separately. The former holds state and needs backups; the latter holds code and needs versioning.
    4. Set the time zone and home directory explicitly. The default time zone inside a container is often UTC, which shifts the times in logs and scheduled jobs; without a fixed home directory, configuration may fail to load.

    After running the command to bring up the orchestration, open the corresponding port on the host in a browser to see the UI.

    5. Pitfalls with Streaming Output and Reverse Proxies

    The core experience of an agent UI is token-by-token output, which relies on server-sent events (SSE) or a long-lived connection. Once a reverse proxy sits in front, the default configuration almost certainly causes problems: the page spins for a long time and then dumps everything at once, or the connection drops midway.

    There are two underlying causes: the proxy enables response buffering by default, so it accumulates a chunk of the streaming response before forwarding it; and the proxy’s default read timeout is short, so long thinking or long generation is treated as a timeout. A usable configuration direction:

    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. proxy_http_version 1.1;
    7. proxy_set_header Connection "";
    8. }

    Notes: proxy_buffering off disables buffering, proxy_cache off prevents caching from breaking streaming semantics, the very long read timeout covers long tasks, and forcing HTTP/1.1 while clearing the Connection header keeps the long connection alive. If there is also a load balancer or content delivery layer in the path, buffering must be disabled and timeouts relaxed at that layer too, otherwise the problem is merely pushed one layer down.

    6. Security Baseline

    Once the UI can write data and execute commands, its privileges are equivalent to the runtime itself, so security configuration cannot be skipped:

    • Always set an access password, using a sufficiently long random one rather than a default value;
    • Prefer binding to the loopback address, route external access through a reverse proxy, and add transport encryption at the proxy layer;
    • Do not put the password in the orchestration file as plain text; provide it through an environment variable file or secret injection;
    • Add rate limiting at the reverse proxy layer to prevent brute-force attempts on the password;
    • Rotate the password regularly and change it immediately when personnel change.

    7. Observability and Troubleshooting

    Since the UI process and the runtime process share one container, troubleshooting starts with figuring out “whose problem it is.” Agree on a unified log location and have both processes write logs to standard output for the container log driver to collect centrally. Common failures and how to locate them:

    Symptom Frequent cause Diagnostic action
    Container exits right after start Startup script not invoked with bash, missing executable bit Check the first error in the container logs
    Port is reachable but page is blank Wrong static asset path or mount directory Check the mounts and directory index
    Output appears all at once Proxy buffering not disabled Verify the proxy buffering and cache settings
    Long task drops midway Read timeout too short Increase the read timeout and check every layer in the path
    Scheduled jobs run at shifted times Container time zone defaults to UTC Set the time zone environment variable explicitly

    8. Working with Cloud Environments

    If this service eventually runs in the cloud, it is best to govern the stateful and stateless parts separately: put mutable data on a cloud disk or object storage for backup, send logs to a log service for centralized search, connect runtime metrics to monitoring and alerting, and let a container platform manage orchestration. This keeps the lightweight advantage of “UI and runtime in the same container” while gaining support for scaling and failure recovery from cloud infrastructure.

    9. Summary

    To add a web UI to an existing agent container, the best solution is often not another generic UI service, but a thin shell next to the runtime process that reuses its module capabilities directly. Four things matter in practice: mount directories and state separately, orchestrate two processes with the main process in the foreground, disable buffering and relax timeouts along the streaming path, and build the security foundation on a password plus loopback binding. With this configuration, the UI experience can be largely on par with the terminal without sacrificing the agent’s unique Skill, Memory, and Cron capabilities.