Spaces:
Running
Running
Create Dockerfile
Browse files- Dockerfile +120 -0
Dockerfile
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM node:20-slim
|
| 2 |
+
|
| 3 |
+
ENV NODE_ENV=production \
|
| 4 |
+
PORT=7860 \
|
| 5 |
+
HOSTNAME=0.0.0.0 \
|
| 6 |
+
NEXT_TELEMETRY_DISABLED=1
|
| 7 |
+
|
| 8 |
+
RUN apt-get update && apt-get install -y --no-install-recommends \
|
| 9 |
+
python3 python3-pip curl procps build-essential ca-certificates \
|
| 10 |
+
&& rm -rf /var/lib/apt/lists/*
|
| 11 |
+
|
| 12 |
+
# 1. Tambahkan instalasi Headroom proxy di sini via pip
|
| 13 |
+
# RUN pip3 install --break-system-packages --no-cache-dir "headroom-ai[proxy]" huggingface_hub
|
| 14 |
+
|
| 15 |
+
RUN pip3 install --break-system-packages --no-cache-dir huggingface_hub
|
| 16 |
+
|
| 17 |
+
RUN npm install -g npm i -g 9router@latest --prefer-online --no-audit --no-fund \
|
| 18 |
+
&& mkdir -p /root/.9router/runtime \
|
| 19 |
+
&& cd /root/.9router/runtime \
|
| 20 |
+
&& npm install sql.js@1.13.0 better-sqlite3@12.6.2 --no-audit --no-fund --prefer-online
|
| 21 |
+
|
| 22 |
+
WORKDIR /app
|
| 23 |
+
|
| 24 |
+
RUN cat > /app/sync.py <<'PY'
|
| 25 |
+
import os
|
| 26 |
+
import tarfile
|
| 27 |
+
import time
|
| 28 |
+
import sys
|
| 29 |
+
from pathlib import Path
|
| 30 |
+
from huggingface_hub import HfApi, hf_hub_download
|
| 31 |
+
|
| 32 |
+
REPO_ID = os.environ.get("DATASET_REPO")
|
| 33 |
+
TOKEN = os.environ.get("HF_TOKEN")
|
| 34 |
+
BACKUP_FILE = "/tmp/9router_backup.tar.gz"
|
| 35 |
+
DATA_DIR = Path("/root/.9router")
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
def safe_extract(tar, path):
|
| 39 |
+
base = Path(path).resolve()
|
| 40 |
+
for member in tar.getmembers():
|
| 41 |
+
target = (base / member.name).resolve()
|
| 42 |
+
if not str(target).startswith(str(base)):
|
| 43 |
+
raise RuntimeError(f"unsafe tar member: {member.name}")
|
| 44 |
+
tar.extractall(path)
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
def upload():
|
| 48 |
+
if not REPO_ID or not TOKEN:
|
| 49 |
+
print(">>> [Sync] DATASET_REPO/HF_TOKEN 未配置,跳过备份")
|
| 50 |
+
return
|
| 51 |
+
if not DATA_DIR.exists():
|
| 52 |
+
print(">>> [Sync] 数据目录不存在,跳过备份")
|
| 53 |
+
return
|
| 54 |
+
try:
|
| 55 |
+
with tarfile.open(BACKUP_FILE, "w:gz") as tar:
|
| 56 |
+
tar.add(str(DATA_DIR), arcname=".9router")
|
| 57 |
+
HfApi(token=TOKEN).upload_file(
|
| 58 |
+
path_or_fileobj=BACKUP_FILE,
|
| 59 |
+
path_in_repo="9router_backup.tar.gz",
|
| 60 |
+
repo_id=REPO_ID,
|
| 61 |
+
repo_type="dataset",
|
| 62 |
+
)
|
| 63 |
+
print(">>> [Sync] 备份成功: " + time.strftime("%Y-%m-%d %H:%M:%S"), flush=True)
|
| 64 |
+
except Exception as e:
|
| 65 |
+
print(">>> [Sync] 备份失败: " + str(e), flush=True)
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
def download():
|
| 69 |
+
if not REPO_ID or not TOKEN:
|
| 70 |
+
print(">>> [Sync] DATASET_REPO/HF_TOKEN 未配置,跳过恢复")
|
| 71 |
+
return
|
| 72 |
+
try:
|
| 73 |
+
print(">>> [Sync] 正在从 Dataset 下载数据...", flush=True)
|
| 74 |
+
path = hf_hub_download(
|
| 75 |
+
repo_id=REPO_ID,
|
| 76 |
+
filename="9router_backup.tar.gz",
|
| 77 |
+
repo_type="dataset",
|
| 78 |
+
token=TOKEN,
|
| 79 |
+
)
|
| 80 |
+
with tarfile.open(path, "r:gz") as tar:
|
| 81 |
+
safe_extract(tar, "/root")
|
| 82 |
+
print(">>> [Sync] 数据恢复成功", flush=True)
|
| 83 |
+
except Exception as e:
|
| 84 |
+
print(">>> [Sync] 跳过恢复: " + str(e), flush=True)
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
if __name__ == "__main__":
|
| 88 |
+
if len(sys.argv) > 1 and sys.argv[1] == "upload":
|
| 89 |
+
upload()
|
| 90 |
+
elif len(sys.argv) > 1 and sys.argv[1] == "download":
|
| 91 |
+
download()
|
| 92 |
+
PY
|
| 93 |
+
|
| 94 |
+
RUN cat > /app/run.sh <<'SH'
|
| 95 |
+
#!/bin/bash
|
| 96 |
+
set -euo pipefail
|
| 97 |
+
|
| 98 |
+
mkdir -p /root/.9router/runtime
|
| 99 |
+
|
| 100 |
+
echo '=== 1. 数据恢复 ==='
|
| 101 |
+
python3 /app/sync.py download
|
| 102 |
+
|
| 103 |
+
echo '=== 2. 启动定时备份 (每10分钟一次) ==='
|
| 104 |
+
(while true; do sleep 600; python3 /app/sync.py upload; done) &
|
| 105 |
+
|
| 106 |
+
echo '=== 3. 启动 9router on 7860 ==='
|
| 107 |
+
export PORT=7860
|
| 108 |
+
export HOSTNAME=0.0.0.0
|
| 109 |
+
export NEXT_PUBLIC_BASE_URL="https://${SPACE_HOST:-jualhosting-9router.hf.space}"
|
| 110 |
+
|
| 111 |
+
# 关键修复:不能用 `printf '\n' | 9router`。
|
| 112 |
+
# 9router 在 stdin 关闭后会触发 SIGINT/退出菜单,HF 看到 Exit code 0 后判定 RUNTIME_ERROR。
|
| 113 |
+
# --tray + --skip-update 会跳过交互菜单,并让 Node 主进程常驻。
|
| 114 |
+
exec 9router --tray --skip-update --no-browser --log --port 7860 --host 0.0.0.0
|
| 115 |
+
SH
|
| 116 |
+
|
| 117 |
+
RUN chmod +x /app/run.sh
|
| 118 |
+
|
| 119 |
+
EXPOSE 7860
|
| 120 |
+
CMD ["/bin/bash", "/app/run.sh"]
|