fix(docker): preserve explicit config selectors during bootstrap

Only initialize the Compose default instance when no --config/-c selector is present. Preserve all CLI-supported selector spellings and cover gateway/API startup against selected persisted settings in the Docker smoke test.
This commit is contained in:
chengyongru authored and chengyongru committed 2026-10-19 13:02:26 +08:00
1 parent b079522fa1
commit 103840171b
3 files changed
+62 -4

No files matched your search

+1 -1
View File
@@ -95,7 +95,7 @@ docker compose logs
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.
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.
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. An explicit `--config` or `-c` selects a user-managed configuration and skips default-instance initialization. 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.
+12 -3
View File
@@ -43,9 +43,18 @@ if [ "$RENDER" = "true" ]; then
fi
set -- "$@" --config "$config"
elif [ "$NANOBOT_DOCKER_BOOTSTRAP" = "1" ] && { [ "$1" = "gateway" ] || [ "$1" = "serve" ]; }; then
config="$dir/config.json"
python -m nanobot.cli.docker_bootstrap "$config" || exit 1
set -- "$@" --config "$config"
# Explicit config selectors belong to the CLI; bootstrap only the default instance.
initialize_default=1
for argument in "$@"; do
case "$argument" in
--config|--config=*|-c*) initialize_default=0; break ;;
esac
done
if [ "$initialize_default" = "1" ]; then
config="$dir/config.json"
python -m nanobot.cli.docker_bootstrap "$config" || exit 1
set -- "$@" --config "$config"
fi
fi
exec nanobot "$@"
+49
View File
@@ -169,6 +169,55 @@ def main() -> None:
"-R", f"{os.getuid()}:{os.getgid()}", "/data",
], check=True)
commands = {name: list(service["command"]) for name, service in config["services"].items()}
selected_path = "/home/nanobot/.nanobot/selected.json"
selectors = (
("long", ["--config", selected_path]),
("long-equals", [f"--config={selected_path}"]),
("short", ["-c", selected_path]),
("short-attached", [f"-c{selected_path}"]),
)
for service_name in ("nanobot-gateway", "nanobot-api"):
for label, selector in selectors:
data = root / f"{service_name}-{label}"
data.mkdir()
selected = data / "selected.json"
selected.write_text(json.dumps(saved), encoding="utf-8")
previous = selected.read_bytes()
for service in config["services"].values():
service["volumes"] = [{
"type": "bind", "source": str(data),
"target": "/home/nanobot/.nanobot",
}]
config["services"][service_name]["command"] = [*commands[service_name], *selector]
compose_path.write_text(json.dumps(config), encoding="utf-8")
try:
run("up", "-d", "--no-deps", service_name)
gateway = service_name == "nanobot-gateway"
health = endpoint(service_name, 18790 if gateway else 8900)
wait_for(health + "/health")
if gateway:
webui = endpoint(service_name, 8765)
assert request(webui + "/webui/bootstrap", password)[0] == 200
else:
assert request(health + "/v1/models", api_key)[0] == 200
assert request(health + "/v1/models", "changed-env")[0] == 401
assert selected.read_bytes() == previous
assert not (data / "config.json").exists()
assert "[entrypoint] initialized" not in run("logs", "--no-color")
print(f"PASS {service_name} {label}: explicit config preserved without default initialization")
except Exception:
print(run("logs", "--no-color", "--tail", "80"), flush=True)
raise
finally:
subprocess.run([*compose, "down", "--volumes"], check=True)
if os.name != "nt":
subprocess.run([
"docker", "run", "--rm", "--entrypoint", "chown",
"-v", f"{data}:/data", args.image,
"-R", f"{os.getuid()}:{os.getgid()}", "/data",
], check=True)
readonly = root / "readonly"
readonly.mkdir()
result = subprocess.run([