开发者
我要评分
获取效率
正确性
完整性
易理解
在线提单
论坛求助

OpenClaw实例部署

  1. 拉取OpenClaw Docker镜像。
    docker pull swr.cn-north-4.myhuaweicloud.com/kunpeng-ai/openclaw-openviking:remote
  2. 可根据实际业务需求,定制化构建镜像。
    1. 保存原镜像的Entrypoint和Cmd配置。
      ORIG_ENTRY=$(docker inspect swr.cn-north-4.myhuaweicloud.com/kunpeng-ai/openclaw-openviking:remote --format="{{json .Config.Entrypoint}}"); ORIG_CMD=$(docker inspect swr.cn-north-4.myhuaweicloud.com/kunpeng-ai/openclaw-openviking:remote --format="{{json .Config.Cmd}}"); echo "原 ENTRYPOINT: $ORIG_ENTRY"; echo "原 CMD: $ORIG_CMD"
    2. 清理并启动临时容器(用tail保持运行,覆盖原Entrypoint)。
      docker rm -f temp-image 2>/dev/null 
      docker run -d --name temp-image --privileged --entrypoint tail swr.cn-north-4.myhuaweicloud.com/kunpeng-ai/openclaw-openviking:remote -f /dev/null
    3. 进入容器使用Yum安装必要组件。
      docker exec temp-image bash -c "yum install -y wget systemd systemd-sysv openssh-server sudo chrony linuxptp socat curl iputils bind-utils iproute nc tcpdump passwd && yum clean all && rm -rf /var/cache/yum /var/tmp/* /tmp/*" 
      docker exec temp-image bash -c 'wget -O /usr/local/bin/websocat https://github.com/vi/websocat/releases/latest/download/websocat.aarch64-unknown-linux-musl && chmod a+x /usr/local/bin/websocat && websocat --version'
    4. 停止容器并导出导入(关键:恢复原来的Entrypoint和Cmd)。
      docker stop temp-image 
      docker export temp-image | docker import -c "ENTRYPOINT $ORIG_ENTRY" -c "CMD $ORIG_CMD" - openclaw-openviking:custom
    5. 清理临时容器。
      docker rm -f temp-image
    6. 推送镜像到Harbor仓库。
      docker tag  openclaw-openviking:custom {本机地址}:2900/e2b-orchestration/openclaw-openviking:custom
      docker push {本机地址}:2900/e2b-orchestration/openclaw-openviking:custom
    7. 在浏览器输入http://{本机地址}:2900/,进入镜像仓Harbor管理界面。用户名为admin,密码为Harbor12345,进入项目可以看到刚刚新创建的镜像。

    8. 终端输入下述命令。
      setenforce 0
    9. 修改“/etc/hosts”,添加以下内容。
      127.0.0.1 harbor
  3. 制作E2B模板并创建沙箱,将E2B_API_URL改为你的服务器的IP地址,端口默认3000。
    import os
    import json
    from e2b import Template, default_build_logger, wait_for_port
    from e2b  import Sandbox
    # 设置E2B环境变量,IP修改为本地IP
    os.environ["E2B_API_URL"] = "http://{你的服务器IP地址}:3000"
    os.environ["E2B_HTTP_SSL"] = "false"
    config_path = "/root/.e2b/config.json"
    if __name__ == '__main__':
        print(f"读取配置文件路径:{config_path}")
        
        # 初始化变量,避免异常时变量未定义
        access_token = None
        team_api_key = None
            # 1. 打开并读取文件内容
        with open(config_path, "r", encoding="utf-8") as f:
            # 2. 解析JSON内容为Python字典
            data = json.load(f)
        # 3. 提取目标字段(使用get方法避免键不存在报错)
        access_token = data.get("accessToken")
        team_api_key = data.get("teamApiKey")
        # 4. 输出结果
        print("提取结果:")
        print(f"accessToken: {access_token}")
        print(f"teamApiKey: {team_api_key}")
        # 验证字段是否存在
        if not access_token or not team_api_key:
            print("警告:文件中未找到accessToken或teamApiKey字段!")
            # 字段缺失时直接退出,避免后续执行失败
            exit(1)
        
        # 设置E2B相关环境变量
        os.environ["E2B_ACCESS_TOKEN"] = access_token
        os.environ["E2B_API_KEY"] = team_api_key
        
        # 构建E2B模板
        print("开始构建E2B模板...")
        Template.build(
            Template().from_dockerfile('FROM harbor:443/e2b-orchestration/openclaw-openviking:custom') # Nginx 反向代理了443端口
            .set_start_cmd("sudo websocat -b --exit-on-eof ws-l:0.0.0.0:8081 tcp:127.0.0.1:22", wait_for_port(8081)), # websocat在沙箱内8081端口启动
            alias="openclaw", # 模板别名
            cpu_count=2,
            memory_mb=2048,
            on_build_logs=default_build_logger(),
            skip_cache=True
        )
        print("模板构建完成!")
        # 沙箱名称与模板名一致
        sbx = Sandbox.create("openclaw")
        print(sbx.commands.run("whoami"))  # guest
        print(sbx.commands.run("ss -tlnp | grep :22", user="root"))
        print("沙箱ID: ", sbx.sandbox_id) #输出沙箱id
  4. 通过SSH连接沙箱。
    1. 安装websocat。
      wget -O /usr/local/bin/websocat https://github.com/vi/websocat/releases/latest/download/websocat.aarch64-unknown-linux-musl
      chmod a+x /usr/local/bin/websocat
    2. 连接沙箱,需替换为实际的沙箱ID(沙箱创建时需记录)。
      export SANDBOX_ID=<上一步输出的沙箱ID> && echo "127.0.0.1 8081-${SANDBOX_ID}.e2b.app" | sudo tee -a /etc/hosts > /dev/null 2>&1 && ssh -o "ProxyCommand=websocat --binary -B 65536 ws://8081-${SANDBOX_ID}.e2b.app" -o "StrictHostKeyChecking=no" -o "UserKnownHostsFile=/dev/null" user@8081-${SANDBOX_ID}.e2b.app