【每日一技】OpenClaw源码安装(Docker简洁实战版)

0 阅读3分钟

【每日一技】OpenClaw源码安装(Docker简洁实战版)

前置条件

  • Docker已安装并运行(Windows/macOS/Linux 均可)
  • 基础 Git 知识(用于克隆代码仓库)
  • 熟悉基本命令行操作(cd、docker build等)

步骤1:构建Docker镜像(准备Node.js环境)

创建目录并构建镜像:

mkdir openclaw
cd openclaw
# 确保Dockerfile已在当前目录
docker build -t ubuntu:24.04.nodejs .

Dockerfile的内容如下:

# Use Ubuntu 24.04 as base image
FROM ubuntu:24.04

# Set environment variables to prevent interactive prompts during build
ENV DEBIAN_FRONTEND=noninteractive
ENV TZ=UTC

# Replace default apt source with Aliyun mirror (China) using deb822 format
# Ubuntu 24.04+ uses /etc/apt/sources.list.d/ubuntu.sources instead of /etc/apt/sources.list
RUN sed -i 's/archive.ubuntu.com/mirrors.aliyun.com/g' /etc/apt/sources.list.d/ubuntu.sources && \
    sed -i 's/security.ubuntu.com/mirrors.aliyun.com/g' /etc/apt/sources.list.d/ubuntu.sources

# Update package list and install essential packages
RUN apt-get update && apt-get install -y \
    vim \
    curl \
    wget \
    git \
    htop \
    net-tools \
    iputils-ping \
    sudo \
    locales \
    tzdata \
    && rm -rf /var/lib/apt/lists/*

# Set locale
RUN locale-gen en_US.UTF-8 && \
    update-locale LANG=en_US.UTF-8
ENV LANG=en_US.UTF-8
ENV LC_ALL=en_US.UTF-8

# Set default command to keep container running
CMD ["/bin/bash"]

说明:

  • ubuntu:24.04.nodejs:基于 Ubuntu 24.04 且预装 Node.js 的自定义镜像
  • -t:指定镜像标签(tag),方便后续引用

步骤 2:启动 OpenCLAW 容器

运行容器并进入交互式 shell:

docker run -it --name openclaw \
  -p 18789:18789 \
  -p 18791:18791 \
  -p 18792:18792 \
  -p 18293:18793 \
  ubuntu:24.04.nodejs

端口映射说明:

  • 18789:Gateway 主服务端口(Web 访问)
  • 18791-18792:其他辅助服务端口
  • 18293→18793:额外服务端口映射

步骤3:从源码安装OpenClaw(5条核心命令)

在容器内执行以下命令:

# 1. 克隆代码仓库
git clone https://github.com/openclaw/openclaw.git
cd openclaw

# 2. 安装依赖并构建UI
pnpm install && pnpm ui:build && pnpm build

# 3. 设置环境(使用bash shell)
SHELL=bash pnpm setup

# 4. 初始化安装(含配置向导)
openclaw onboard --install-daemon

配置提示: 执行完上述命令后,系统会引导您配置以下关键项:

  • LLM provider(大语言模型提供商,如 OpenAI、Anthropic 等)
  • Web search provider(网络搜索服务商)
  • 其他必要参数(按实际提示填写)

步骤4:配置Gateway(监听所有IP)

修改配置文件使 Gateway 可被外部访问:

# 使用 nano 编辑器打开配置文件
vi /root/.openclaw/openclaw.json

找到 gateway->bind 字段,把bind的值修改lan,并保存:

"gateway": {
    "port": 18789,
    "mode": "local",
    "bind": "lan",  // 改为lan,允许局域网访问

步骤5:启动Gateway服务

openclaw gateway

说明: Gateway是OpenCLAW的核心服务,负责处理所有请求转发和 API 调用。

步骤 6:获取Dashboard访问链接

openclaw dashboard --no-open

说明:

  • --no-open:不自动打开浏览器,仅输出 URL
  • 终端会显示类似 http://localhost:18789/dashboard/xxx 的访问地址,示例如下:

20260322-openclaw-docker-install-from-source.png

步骤7:解决设备配对问题(如遇报错)

如果在浏览器访问时遇到 "pairing required" 错误:

# 1. 列出待配对设备
openclaw devices list

![20260322-openclaw-docker-install-from-source-1](./image/20260322-openclaw-docker-install-from-source-1.png)

# 2. 批准设备(替换为实际显示的 ID)
openclaw devices approve 40bc4fe0-55a9-4c39-82b7-a1455213fd07

20260322-openclaw-docker-install-from-source-1.png 20260322-openclaw-docker-install-from-source-2.png

注意事项:

  • 设备ID每次可能不同,以 openclaw devices list 输出的实际值为准
  • 如果列表为空,请检查Gateway是否正常运行

步骤8:测试

20260322-openclaw-docker-install-from-source-3.png

20260322-openclaw-docker-install-from-source-4.png