fix(docker): validate and atomically save first-boot configuration

This commit is contained in:
chengyongru authored and chengyongru committed 2026-10-19 13:02:26 +08:00
1 parent ef2cd0c8c7
commit e20737a509
8 files changed
+314 -168

No files matched your search

+41 -49
View File
@@ -65,7 +65,7 @@ If deployment fails, open the service **Logs** page first. A missing model key f
> Official Docker usage currently means building from this repository with the included `Dockerfile`. Docker Hub images under third-party namespaces are not maintained or verified by HKUDS/nanobot; do not mount API keys or bot tokens into them unless you trust the publisher.
> [!IMPORTANT]
> The gateway and WebSocket channel default to `host: "127.0.0.1"` in `config.json` (set in `nanobot/config/schema.py`). Docker `-p` port forwarding cannot reach a container's loopback interface, so for the host or LAN to reach the exposed ports you must set both binds to `0.0.0.0` in `~/.nanobot/config.json` before starting the container. To serve the bundled WebUI from Docker, bind the WebSocket channel externally and protect bootstrap with `tokenIssueSecret`:
> The gateway and WebSocket channel default to `host: "127.0.0.1"` in `config.json` (set in `nanobot/config/schema.py`). Docker `-p` port forwarding cannot reach a container's loopback interface, so for the host or LAN to reach the exposed ports existing configurations and manual Docker runs must set both binds to `0.0.0.0` in `~/.nanobot/config.json` before starting the container. The Compose first-boot setup below sets these automatically. To serve the bundled WebUI from Docker, bind the WebSocket channel externally and protect bootstrap with `tokenIssueSecret`:
>
> ```json
> {
@@ -82,31 +82,6 @@ If deployment fails, open the service **Logs** page first. A missing model key f
>
> When the WebSocket `host` is `0.0.0.0`, the channel refuses to start unless `token`, `tokenIssueSecret`, or a fully configured `trustedProxyAuth` is also configured. See [`webui.md#lan-access`](./webui.md#lan-access) for details. The gateway health route itself is intentionally minimal and unauthenticated. When the container binds it to `0.0.0.0`, publish port `18790` to host loopback only; place any remotely monitored health endpoint behind a firewall or reverse proxy. If another host must probe it directly, replace `127.0.0.1` in the port mapping with a trusted host interface and restrict inbound traffic to the monitoring system.
### Cloudflare Tunnel + Cloudflare Access
For a local `cloudflared` process in front of nanobot, Cloudflare Access can authenticate the user before forwarding the request and add `Cf-Access-Jwt-Assertion`. Opt in to trusted-proxy no-token mode only when the direct TCP peer is the tunnel process and the assertion is non-empty:
```json
{
"gateway": { "host": "127.0.0.1" },
"channels": {
"websocket": {
"host": "127.0.0.1",
"port": 8765,
"publicWsUrl": "wss://nanobot.example.com/",
"trustedProxyAuth": {
"trustedPeerCidrs": ["127.0.0.1/32", "::1/128"],
"assertionHeader": "Cf-Access-Jwt-Assertion"
}
}
}
}
```
This is two-part authorization: a trusted direct loopback peer **and** a non-empty Cloudflare Access assertion. A trusted CIDR alone is not a bypass. For this flow `/webui/bootstrap` returns connection metadata without a bootstrap token or REST API token; the proxy assertion authorizes the WebSocket handshake and REST requests directly.
Set `publicWsUrl` to the browser-facing `wss://` endpoint when the tunnel sends the origin host header (such as `127.0.0.1:8765`); otherwise the WebUI could attempt to open its WebSocket directly against the loopback address. The assertion header must be generated by Cloudflare Access after authentication; routing/client metadata headers such as `Host`, `Forwarded`, `X-Forwarded-*`, `X-Real-IP`, and `CF-Connecting-IP` are rejected as `assertionHeader` values. Nanobot trusts the assertion but does not cryptographically validate the JWT, so configure the tunnel and Access policy carefully and do not expose the nanobot listener directly to untrusted clients. Forwarded client headers do not establish proxy trust.
### Docker Compose
On a new Linux host, install Git and Docker with the Compose plugin, then get the repository. You do not need to install Python or nanobot on the host:
@@ -114,14 +89,25 @@ On a new Linux host, install Git and Docker with the Compose plugin, then get th
```bash
git clone https://github.com/HKUDS/nanobot.git
cd nanobot
docker compose build
docker compose run --rm nanobot-cli onboard
docker compose up -d
docker compose logs nanobot-gateway
```
The last command initializes `~/.nanobot/config.json` and the workspace in the bind mount. Skip it when reusing an existing configuration. Edit that file on the host and merge your provider settings from [provider setup](./providers.md); select the provider/model you can access, either through the documented model preset or direct `agents.defaults.provider` and `agents.defaults.model` settings. Keep `agents.defaults.workspace` as a container path, normally `~/.nanobot/workspace` (resolved inside the container), rather than a host home-directory path.
On first boot, Compose initializes `~/.nanobot/config.json` with container bind addresses and random WebUI/API credentials. Open `http://HOST:8765`, enter the WebUI password printed in the first-boot logs, then configure your provider and model in **Settings → Models**. Send a message to verify the setup. No provider is required just to open the WebUI.
Also merge the Docker bind and authentication settings [above](#docker): `gateway.host` and `channels.websocket.host` must be `0.0.0.0`, and `tokenIssueSecret` must be a private random value. These snippets are partial configuration, not replacements for your provider settings. Config `${VAR}` references require those variables inside every CLI and gateway container; Compose does not forward arbitrary host variables automatically. Either add the necessary `environment` entries to the services or keep credentials in the protected config file.
Export `NANOBOT_WEB_TOKEN` or `NANOBOT_API_KEY` before the first startup to supply your own credentials. Compose forwards these variables into the containers. Existing configuration is left untouched, including credentials and bind addresses; changing these variables later does not rotate persisted credentials. If you reuse an existing configuration, check the [Docker bind and authentication settings](#docker) above.
The configuration and workspace persist in the `~/.nanobot` bind mount. Keep `agents.defaults.workspace` as a container path, normally `~/.nanobot/workspace`, rather than a host home-directory path. Other config `${VAR}` references require explicit `environment` entries in the CLI and gateway services; Compose does not forward arbitrary host variables.
The OpenAI-compatible API is optional and requires a configured provider. After completing model setup, enable it with:
```bash
docker compose --profile api up -d
```
Its endpoint is `http://127.0.0.1:8900/v1`; use the API key from the first-boot logs or `api.apiKey` in the persisted config. It is published to host loopback only.
For terminal setup, run `docker compose run --rm nanobot-cli onboard --wizard` and preserve existing settings. Verify it with `docker compose run --rm nanobot-cli agent -m "Hello!"` before starting the persistent gateway.
The default image preinstalls WhatsApp dependencies. To bake other enabled channels into an image (recommended for deployments without PyPI access), pass a comma-separated `NANOBOT_CHANNELS` build argument:
@@ -131,26 +117,7 @@ NANOBOT_CHANNELS=telegram,slack docker compose build
The image keeps nanobot in a virtual environment owned by its built-in non-root runtime user (UID 1000). If an enabled channel was not preinstalled, gateway startup can therefore install its manifest-declared dependencies. Rebuilding with `NANOBOT_CHANNELS` keeps that installation reproducible instead of relying on the container's writable layer. If you override the container with a different `--user`, bake every enabled channel into the image because that UID is not guaranteed write access to the virtual environment.
The first `docker compose up` bootstraps `~/.nanobot/config.json` with container-appropriate binds and generated secrets: the WebUI password (`NANOBOT_WEB_TOKEN`) and the OpenAI-compatible API key (`NANOBOT_API_KEY`). Export either variable before `up` to pin your own; otherwise the entrypoint generates one and prints it to the container log. An existing `config.json` is left untouched. Add provider keys afterwards in the WebUI settings or with `onboard`:
```bash
docker compose up -d nanobot-gateway # first boot writes ~/.nanobot/config.json
docker compose logs nanobot-gateway # shows the generated WebUI password and API key
```
The `nanobot-api` service (OpenAI-compatible endpoint on `127.0.0.1:8900`) needs a configured provider before it can serve, so it is opt-in via the `api` profile:
```bash
docker compose --profile api up -d # also start the API once a provider is configured
```
For a fully pre-configured setup, keep using first-time onboarding:
```bash
docker compose run --rm nanobot-cli status
docker compose run --rm nanobot-cli agent -m "Hello!"
# Continue only after a normal reply.
docker compose up -d nanobot-gateway
curl --fail http://127.0.0.1:18790/health
```
@@ -222,6 +189,31 @@ docker run -v ~/.nanobot:/home/nanobot/.nanobot --rm nanobot agent -m "Hello!"
docker run -v ~/.nanobot:/home/nanobot/.nanobot --rm nanobot status
```
### Cloudflare Tunnel + Cloudflare Access
For a local `cloudflared` process in front of nanobot, Cloudflare Access can authenticate the user before forwarding the request and add `Cf-Access-Jwt-Assertion`. Opt in to trusted-proxy no-token mode only when the direct TCP peer is the tunnel process and the assertion is non-empty:
```json
{
"gateway": { "host": "127.0.0.1" },
"channels": {
"websocket": {
"host": "127.0.0.1",
"port": 8765,
"publicWsUrl": "wss://nanobot.example.com/",
"trustedProxyAuth": {
"trustedPeerCidrs": ["127.0.0.1/32", "::1/128"],
"assertionHeader": "Cf-Access-Jwt-Assertion"
}
}
}
}
```
This is two-part authorization: a trusted direct loopback peer **and** a non-empty Cloudflare Access assertion. A trusted CIDR alone is not a bypass. For this flow `/webui/bootstrap` returns connection metadata without a bootstrap token or REST API token; the proxy assertion authorizes the WebSocket handshake and REST requests directly.
Set `publicWsUrl` to the browser-facing `wss://` endpoint when the tunnel sends the origin host header (such as `127.0.0.1:8765`); otherwise the WebUI could attempt to open its WebSocket directly against the loopback address. The assertion header must be generated by Cloudflare Access after authentication; routing/client metadata headers such as `Host`, `Forwarded`, `X-Forwarded-*`, `X-Real-IP`, and `CF-Connecting-IP` are rejected as `assertionHeader` values. Nanobot trusts the assertion but does not cryptographically validate the JWT, so configure the tunnel and Access policy carefully and do not expose the nanobot listener directly to untrusted clients. Forwarded client headers do not establish proxy trust.
## Switch an Existing Gateway to a Service
Before installing an OS service, finish active work and close WebUI/TUI launchers that could start another gateway. Stop the existing instance: use `nanobot gateway stop` for a recorded background gateway (including one retained by `/detach`), or Ctrl+C in the terminal running a foreground gateway. Pass the same `--config` and `--workspace` selectors to stop, status, and service-install commands for a custom instance.