Create Dockerfile
Browse files- Dockerfile +99 -0
Dockerfile
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM node:22-slim
|
| 2 |
+
|
| 3 |
+
# 1. 基础依赖补全
|
| 4 |
+
RUN apt-get update && apt-get install -y --no-install-recommends \
|
| 5 |
+
git openssh-client build-essential python3 python3-pip curl wget \
|
| 6 |
+
g++ make ca-certificates \
|
| 7 |
+
&& rm -rf /var/lib/apt/lists/*
|
| 8 |
+
|
| 9 |
+
# 2. 安装 HF 数据交互工具
|
| 10 |
+
RUN pip3 install --no-cache-dir huggingface_hub --break-system-packages
|
| 11 |
+
|
| 12 |
+
# 3. 构建环境与 Git 协议优化
|
| 13 |
+
RUN update-ca-certificates && \
|
| 14 |
+
git config --global http.sslVerify false && \
|
| 15 |
+
git config --global url."https://github.com/".insteadOf ssh://git@github.com/
|
| 16 |
+
|
| 17 |
+
# 4. OpenClaw 核心安装
|
| 18 |
+
RUN npm install -g openclaw@latest --unsafe-perm
|
| 19 |
+
|
| 20 |
+
# 5. 环境变量预设
|
| 21 |
+
ENV PORT=7860 \
|
| 22 |
+
OPENCLAW_GATEWAY_MODE=local \
|
| 23 |
+
HOME=/root
|
| 24 |
+
|
| 25 |
+
# 6. Python 同步引擎 (sync.py)
|
| 26 |
+
RUN echo 'import os, sys, tarfile\n\
|
| 27 |
+
from huggingface_hub import HfApi, hf_hub_download\n\
|
| 28 |
+
from datetime import datetime, timedelta\n\
|
| 29 |
+
api = HfApi()\n\
|
| 30 |
+
repo_id = os.getenv("HF_DATASET")\n\
|
| 31 |
+
token = os.getenv("HF_TOKEN")\n\
|
| 32 |
+
def restore():\n\
|
| 33 |
+
try:\n\
|
| 34 |
+
files = api.list_repo_files(repo_id=repo_id, repo_type="dataset", token=token)\n\
|
| 35 |
+
now = datetime.now()\n\
|
| 36 |
+
for i in range(5):\n\
|
| 37 |
+
day = (now - timedelta(days=i)).strftime("%Y-%m-%d")\n\
|
| 38 |
+
name = f"backup_{day}.tar.gz"\n\
|
| 39 |
+
if name in files:\n\
|
| 40 |
+
path = hf_hub_download(repo_id=repo_id, filename=name, repo_type="dataset", token=token)\n\
|
| 41 |
+
with tarfile.open(path, "r:gz") as tar: tar.extractall(path="/root/.openclaw/")\n\
|
| 42 |
+
print(f"Success: Restored from {name}")\n\
|
| 43 |
+
return True\n\
|
| 44 |
+
except Exception as e: print(f"Restore Error: {e}")\n\
|
| 45 |
+
def backup():\n\
|
| 46 |
+
try:\n\
|
| 47 |
+
day = datetime.now().strftime("%Y-%m-%d")\n\
|
| 48 |
+
name = f"backup_{day}.tar.gz"\n\
|
| 49 |
+
with tarfile.open(name, "w:gz") as tar:\n\
|
| 50 |
+
if os.path.exists("/root/.openclaw/sessions"): tar.add("/root/.openclaw/sessions", arcname="sessions")\n\
|
| 51 |
+
tar.add("/root/.openclaw/openclaw.json", arcname="openclaw.json")\n\
|
| 52 |
+
api.upload_file(path_or_fileobj=name, path_in_repo=name, repo_id=repo_id, repo_type="dataset", token=token)\n\
|
| 53 |
+
print(f"Backup {name} Success.")\n\
|
| 54 |
+
except Exception as e: print(f"Backup Error: {e}")\n\
|
| 55 |
+
if __name__ == "__main__":\n\
|
| 56 |
+
if len(sys.argv) > 1 and sys.argv[1] == "backup": backup()\n\
|
| 57 |
+
else: restore()' > /usr/local/bin/sync.py
|
| 58 |
+
|
| 59 |
+
# 7. 启动控制逻辑
|
| 60 |
+
RUN echo "#!/bin/bash\n\
|
| 61 |
+
set -e\n\
|
| 62 |
+
mkdir -p /root/.openclaw/sessions\n\
|
| 63 |
+
\n\
|
| 64 |
+
# 阶段 3: 执行启动前恢复\n\
|
| 65 |
+
python3 /usr/local/bin/sync.py restore\n\
|
| 66 |
+
\n\
|
| 67 |
+
# 处理地址逻辑\n\
|
| 68 |
+
CLEAN_BASE=\"https://api-inference.huggingface.co/models/\$MODEL/v1\"\n\
|
| 69 |
+
\n\
|
| 70 |
+
# 阶段 2: 生成网关与模型配置\n\
|
| 71 |
+
cat > /root/.openclaw/openclaw.json <<EOF\n\
|
| 72 |
+
{\n\
|
| 73 |
+
\"models\": {\n\
|
| 74 |
+
\"providers\": {\n\
|
| 75 |
+
\"huggingface\": {\n\
|
| 76 |
+
\"baseUrl\": \"\$CLEAN_BASE\", \"apiKey\": \"\$HF_TOKEN\", \"api\": \"openai-completions\",\n\
|
| 77 |
+
\"models\": [{ \"id\": \"\$MODEL\", \"name\": \"\$MODEL\", \"contextWindow\": 128000 }]\n\
|
| 78 |
+
}\n\
|
| 79 |
+
}\n\
|
| 80 |
+
},\n\
|
| 81 |
+
\"agents\": { \"defaults\": { \"model\": { \"primary\": \"huggingface/\$MODEL\" } } },\n\
|
| 82 |
+
\"gateway\": {\n\
|
| 83 |
+
\"mode\": \"local\", \"bind\": \"lan\", \"port\": \$PORT,\n\
|
| 84 |
+
\"trustedProxies\": [\"0.0.0.0/0\", \"10.0.0.0/8\", \"172.16.0.0/12\", \"192.168.0.0/16\"],\n\
|
| 85 |
+
\"auth\": { \"mode\": \"token\", \"token\": \"\$OPENCLAW_GATEWAY_PASSWORD\" },\n\
|
| 86 |
+
\"controlUi\": { \"allowInsecureAuth\": true }\n\
|
| 87 |
+
}\n\
|
| 88 |
+
}\n\
|
| 89 |
+
EOF\n\
|
| 90 |
+
\n\
|
| 91 |
+
# 增量备份循环 (每 6 小时)\n\
|
| 92 |
+
(while true; do sleep 21600; python3 /usr/local/bin/sync.py backup; done) &\n\
|
| 93 |
+
\n\
|
| 94 |
+
openclaw doctor --fix\n\
|
| 95 |
+
exec openclaw gateway run --port \$PORT\n\
|
| 96 |
+
" > /usr/local/bin/start-openclaw && chmod +x /usr/local/bin/start-openclaw
|
| 97 |
+
|
| 98 |
+
EXPOSE 7860
|
| 99 |
+
CMD ["/usr/local/bin/start-openclaw"]
|