使用Docker安装nanobot(超轻量个人AI助手)

1、安装Docker

bash 复制代码
详见:https://mp.weixin.qq.com/s/CrjQTLJM0YbJ9SC4GIfKsg

2、安装nanobot

bash 复制代码
详见:https://github.com/HKUDS/nanobot

克隆仓库:

bash 复制代码
# 如果没有安装git,请先安装

# 方式1:未使用代理下载
git clone https://github.com/HKUDS/nanobot

# 方式2:使用代理下载
# 详见:https://mp.weixin.qq.com/s/XTbT3wKp_W2fiK7pGpo7Qg
git clone https://gh-proxy.com/https://github.com/HKUDS/nanobot

切换目录:

bash 复制代码
cd nanobot

查看docker-compose.yml文件:

bash 复制代码
详见:https://github.com/HKUDS/nanobot/blob/main/docker-compose.yml
bash 复制代码
x-common-config: &common-config
  build:
    context: .
    dockerfile: Dockerfile
  volumes:
    - ~/.nanobot:/root/.nanobot
services:
  nanobot-gateway:
    container_name: nanobot-gateway
    <<: *common-config
    command: ["gateway"]
    restart: unless-stopped
    ports:
      - 18790:18790
    deploy:
      resources:
        limits:
          cpus: '1'
          memory: 1G
        reservations:
          cpus: '0.25'
          memory: 256M

  nanobot-cli:
    <<: *common-config
    profiles:
      - cli
    command: ["status"]
    stdin_open: true
    tty: true

备份docker-compose.yml文件:

bash 复制代码
cp docker-compose.yml docker-compose.yml-bak

修改docker-compose.yml文件:

bash 复制代码
# 修改数据卷:
sed -i 's#~/.nanobot#./data#g' docker-compose.yml

查看Dockerfile文件:

bash 复制代码
详见:https://github.com/HKUDS/nanobot/blob/main/Dockerfile
bash 复制代码
FROM ghcr.io/astral-sh/uv:python3.12-bookworm-slim

# Install Node.js 20 for the WhatsApp bridge
RUN apt-get update && \
    apt-get install -y --no-install-recommends curl ca-certificates gnupg git openssh-client && \
    mkdir -p /etc/apt/keyrings && \
    curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg && \
    echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_20.x nodistro main" > /etc/apt/sources.list.d/nodesource.list && \
    apt-get update && \
    apt-get install -y --no-install-recommends nodejs && \
    apt-get purge -y gnupg && \
    apt-get autoremove -y && \
    rm -rf /var/lib/apt/lists/*

WORKDIR /app

# Install Python dependencies first (cached layer)
COPY pyproject.toml README.md LICENSE ./
RUN mkdir -p nanobot bridge && touch nanobot/__init__.py && \
    uv pip install --system --no-cache . && \
    rm -rf nanobot bridge

# Copy the full source and install
COPY nanobot/ nanobot/
COPY bridge/ bridge/
RUN uv pip install --system --no-cache .

# Build the WhatsApp bridge
RUN git config --global url."https://github.com/".insteadOf "ssh://git@github.com/"

WORKDIR /app/bridge
RUN npm install && npm run build
WORKDIR /app

# Create config directory
RUN mkdir -p /root/.nanobot

# Gateway default port
EXPOSE 18790

ENTRYPOINT ["nanobot"]
CMD ["status"]

备份Dockerfile文件:

bash 复制代码
cp Dockerfile Dockerfile-bak

修改Dockerfile文件:

bash 复制代码
# 修改python镜像源:
# 详见:https://mirrors.nju.edu.cn/
FROM ghcr.nju.edu.cn/astral-sh/uv:python3.12-bookworm-slim

ENV APT_MIRROR=mirrors.aliyun.com
ENV NODEJS_URL=https://mirrors.aliyun.com/nodejs-release/v20.19.2/node-v20.19.2-linux-x64.tar.gz
ENV PIP_INDEX_URL=https://mirrors.aliyun.com/pypi/simple/
ENV NPM_REGISTRY=https://registry.npmmirror.com

# Install Node.js 20 for the WhatsApp bridge
# 修改debian镜像源:
# 详见:https://developer.aliyun.com/mirror/debian
# 详见:https://mirrors.tencent.com/help/debian.html
RUN sed -i "s/deb.debian.org/${APT_MIRROR}/g" /etc/apt/sources.list.d/debian.sources && \
    apt-get update && \
    apt-get install -y --no-install-recommends curl ca-certificates gnupg git openssh-client && \
    curl -fsSL ${NODEJS_URL} | \
    tar -xz -C /usr/local --strip-components=1 && \
    apt-get autoremove -y && \
    rm -rf /var/lib/apt/lists/*

WORKDIR /app

# Install Python dependencies first (cached layer)
COPY pyproject.toml README.md LICENSE ./

# 修改pypi镜像源:
# 详见:https://developer.aliyun.com/mirror/pypi
RUN pip config set global.index-url ${PIP_INDEX_URL} && \
    mkdir -p nanobot bridge && touch nanobot/__init__.py && \
    uv pip install --index-url ${PIP_INDEX_URL} --system --no-cache . && \
    rm -rf nanobot bridge

# Copy the full source and install
COPY nanobot/ nanobot/
COPY bridge/ bridge/

# 修改pypi镜像源:
# 详见:https://developer.aliyun.com/mirror/pypi
RUN uv pip install --index-url ${PIP_INDEX_URL} --system --no-cache .

# Build the WhatsApp bridge
RUN git config --global url."https://github.com/".insteadOf "ssh://git@github.com/"

WORKDIR /app/bridge

# 修改npm镜像源:
# 详见:https://developer.aliyun.com/mirror/NPM
RUN npm config set registry ${NPM_REGISTRY} && \
    npm install && npm run build
WORKDIR /app

# Create config directory
RUN mkdir -p /root/.nanobot

# Gateway default port
EXPOSE 18790

ENTRYPOINT ["nanobot"]
CMD ["status"]

创建并启动容器:

bash 复制代码
"首次安装"

# 1、执行命令
docker-compose run --rm nanobot-cli onboard

# 2、添加API keys
# 当前使用"Cloudflare的AI Gateway代理Google AI Studio"
# 详见:https://mp.weixin.qq.com/s/Xg7W7KJs6IpvXxPCoMWtXw
修改"./data/config.json"文件的部分内容,示例如下:
{
  "providers": {
    "custom": {
      "apiBase": "https://gateway.ai.cloudflare.com/v1/{填写ACCOUNT_ID}/gemini/compat",
      "apiKey": "{填写API_KEY}"
    }
  },
  "agents": {
    "defaults": {
      "provider": "custom",
      "model": "google-ai-studio/gemini-2.5-flash"
    }
  }
}

# 3、配置Channel
# 详见:https://github.com/HKUDS/nanobot#-chat-apps
修改"./data/config.json"文件的部分内容,以飞书为例(当前暂未配置):
{
  "channels": {
    "feishu": {
      "enabled": true,
      "appId": "cli_xxx",
      "appSecret": "xxx",
      "encryptKey": "",
      "verificationToken": "",
      "allowFrom": ["ou_YOUR_OPEN_ID"],
      "groupPolicy": "mention"
    }
  }
}
bash 复制代码
"创建并启动容器"

# 执行命令:
docker compose up -d nanobot-gateway

查看容器列表:

复制代码
docker ps

进入容器:

bash 复制代码
# 方式1:

# 进入容器:
docker exec -it nanobot-gateway bash

# 执行命令:
nanobot agent

# 退出cli交互、退出容器:
exit
bash 复制代码
# 方式1:

# 进入容器:
docker exec -it nanobot-gateway bash

# 执行命令:
nanobot agent

# 退出cli交互、退出容器:
exit

停止并销毁容器:

bash 复制代码
docker-compose down

删除目录:

bash 复制代码
rm -rf ./data

删除镜像:

bash 复制代码
docker rmi nanobot-nanobot-cli nanobot-nanobot-gateway

删除所有未使用的构建缓存(Build Cache):

bash 复制代码
docker builder prune

3、详见

bash 复制代码
https://github.com/HKUDS/nanobot
https://mp.weixin.qq.com/s/7d-baWyFDVoPS7Z67yBaBw
相关推荐
恋红尘3 小时前
K8S 配置与调度-叩丁狼
云原生·容器·kubernetes
喵喵爱自由3 小时前
Docker容器共享宿主机-安全网络
网络·安全·docker
杨浦老苏4 小时前
搜索引擎聚合器Degoog
搜索引擎·docker·群晖
恋红尘4 小时前
K8S Pod 基础解析-分篇-叩丁狼
云原生·容器·kubernetes·pod
丘桔5 小时前
k8s01:容器运行时之争
云原生·容器·kubernetes
Mr_Chenph6 小时前
OpenClaw 3.13 Skill编写初探(Docker)
docker·skill·openclaw
学不完的6 小时前
Docker 的安全优化
运维·安全·docker·容器·eureka
恋红尘7 小时前
K8S 服务发现-叩丁狼
linux·docker·kubernetes
sleP4o7 小时前
[Docker] ‘s Basic Usage
docker·容器·eureka