Update Dockerfile
Browse files- Dockerfile +6 -125
Dockerfile
CHANGED
@@ -10,143 +10,24 @@ RUN apt-get update && \
|
|
10 |
RUN git clone https://github.com/ErlichLiu/DeepClaude.git /app
|
11 |
WORKDIR /app
|
12 |
|
13 |
-
#
|
14 |
-
RUN pip install --no-cache-dir fastapi uvicorn python-dotenv huggingface_hub
|
15 |
|
16 |
# 如果仓库中有requirements.txt,也安装其中的依赖
|
17 |
RUN if [ -f requirements.txt ]; then \
|
18 |
pip install --no-cache-dir -r requirements.txt; \
|
19 |
fi
|
20 |
|
21 |
-
#
|
22 |
-
RUN mkdir -p /app/data
|
23 |
|
24 |
# 创建.env文件
|
25 |
RUN touch .env
|
26 |
|
27 |
-
#
|
28 |
-
COPY
|
29 |
-
#!/bin/bash
|
30 |
-
|
31 |
-
# 检查环境变量
|
32 |
-
if [ -z "\$HF_TOKEN" ] || [ -z "\$DATASET_ID" ]; then
|
33 |
-
echo "Starting without backup functionality - missing HF_TOKEN or DATASET_ID"
|
34 |
-
exec uvicorn app.main:app --host 0.0.0.0 --port 7860
|
35 |
-
exit 0
|
36 |
-
fi
|
37 |
-
|
38 |
-
# 登录HuggingFace (使用环境变量方式避免交互问题)
|
39 |
-
export HUGGING_FACE_HUB_TOKEN=\$HF_TOKEN
|
40 |
-
|
41 |
-
# 确保data目录存在
|
42 |
-
mkdir -p /app/data
|
43 |
-
|
44 |
-
# 同步函数
|
45 |
-
sync_data() {
|
46 |
-
while true; do
|
47 |
-
echo "Starting sync process at \$(date)"
|
48 |
-
|
49 |
-
# 创建临时压缩文件
|
50 |
-
cd /app
|
51 |
-
timestamp=\$(date +%Y%m%d_%H%M%S)
|
52 |
-
backup_file="backup_\${timestamp}.tar.gz"
|
53 |
-
|
54 |
-
# 检查data目录是否存在且不为空
|
55 |
-
if [ -d "data" ] && [ "\$(ls -A data 2>/dev/null)" ]; then
|
56 |
-
tar -czf "/tmp/\${backup_file}" data/
|
57 |
-
|
58 |
-
# 上传备份并管理历史备份
|
59 |
-
python3 -c "
|
60 |
-
from huggingface_hub import HfApi
|
61 |
-
import os
|
62 |
-
def manage_backups(api, repo_id, max_files=50):
|
63 |
-
files = api.list_repo_files(repo_id=repo_id, repo_type='dataset')
|
64 |
-
backup_files = [f for f in files if f.startswith('backup_') and f.endswith('.tar.gz')]
|
65 |
-
backup_files.sort()
|
66 |
-
|
67 |
-
if len(backup_files) >= max_files:
|
68 |
-
files_to_delete = backup_files[:(len(backup_files) - max_files + 1)]
|
69 |
-
for file_to_delete in files_to_delete:
|
70 |
-
try:
|
71 |
-
api.delete_file(path_in_repo=file_to_delete, repo_id=repo_id, repo_type='dataset')
|
72 |
-
print(f'Deleted old backup: {file_to_delete}')
|
73 |
-
except Exception as e:
|
74 |
-
print(f'Error deleting {file_to_delete}: {str(e)}')
|
75 |
-
try:
|
76 |
-
api = HfApi()
|
77 |
-
api.upload_file(
|
78 |
-
path_or_fileobj='/tmp/\${backup_file}',
|
79 |
-
path_in_repo='\${backup_file}',
|
80 |
-
repo_id='\${DATASET_ID}',
|
81 |
-
repo_type='dataset'
|
82 |
-
)
|
83 |
-
print('Backup uploaded successfully')
|
84 |
-
|
85 |
-
manage_backups(api, '\${DATASET_ID}')
|
86 |
-
except Exception as e:
|
87 |
-
print(f'Backup failed: {str(e)}')
|
88 |
-
"
|
89 |
-
# 清理临时文件
|
90 |
-
rm -f "/tmp/\${backup_file}"
|
91 |
-
echo "Backup completed"
|
92 |
-
else
|
93 |
-
echo "No data to backup or data directory not found"
|
94 |
-
fi
|
95 |
-
|
96 |
-
# 设置同步间隔
|
97 |
-
SYNC_INTERVAL=\${SYNC_INTERVAL:-7200}
|
98 |
-
echo "Next sync in \${SYNC_INTERVAL} seconds..."
|
99 |
-
sleep \$SYNC_INTERVAL
|
100 |
-
done
|
101 |
-
}
|
102 |
-
|
103 |
-
# 恢复函数
|
104 |
-
restore_latest() {
|
105 |
-
echo "Attempting to restore latest backup..."
|
106 |
-
python3 -c "
|
107 |
-
try:
|
108 |
-
from huggingface_hub import HfApi
|
109 |
-
import os
|
110 |
-
|
111 |
-
api = HfApi()
|
112 |
-
files = api.list_repo_files('\${DATASET_ID}', repo_type='dataset')
|
113 |
-
backup_files = [f for f in files if f.startswith('backup_') and f.endswith('.tar.gz')]
|
114 |
-
|
115 |
-
if backup_files:
|
116 |
-
latest = sorted(backup_files)[-1]
|
117 |
-
api.hf_hub_download(
|
118 |
-
repo_id='\${DATASET_ID}',
|
119 |
-
filename=latest,
|
120 |
-
repo_type='dataset',
|
121 |
-
local_dir='/tmp'
|
122 |
-
)
|
123 |
-
os.system(f'tar -xzf /tmp/{latest} -C /app')
|
124 |
-
os.remove(f'/tmp/{latest}')
|
125 |
-
print(f'Restored from {latest}')
|
126 |
-
else:
|
127 |
-
print('No backup found')
|
128 |
-
except Exception as e:
|
129 |
-
print(f'Restore failed: {str(e)}')
|
130 |
-
"
|
131 |
-
}
|
132 |
-
|
133 |
-
# 主程序
|
134 |
-
(
|
135 |
-
# 尝试恢复
|
136 |
-
restore_latest
|
137 |
-
|
138 |
-
# 启动同步进程
|
139 |
-
sync_data &
|
140 |
-
|
141 |
-
# 启动主应用
|
142 |
-
exec uvicorn app.main:app --host 0.0.0.0 --port 7860
|
143 |
-
) 2>&1 | tee -a /app/data/backup.log
|
144 |
-
EOF
|
145 |
-
|
146 |
-
# 确保脚本可执行
|
147 |
RUN chmod +x /app/sync_data.sh
|
148 |
|
149 |
-
# 暴露Hugging Face默认端口
|
150 |
EXPOSE 7860
|
151 |
|
152 |
# 使用备份脚本启动服务
|
|
|
10 |
RUN git clone https://github.com/ErlichLiu/DeepClaude.git /app
|
11 |
WORKDIR /app
|
12 |
|
13 |
+
# 安装Python依赖(包括新增的 colorlog)
|
14 |
+
RUN pip install --no-cache-dir fastapi uvicorn python-dotenv huggingface_hub colorlog
|
15 |
|
16 |
# 如果仓库中有requirements.txt,也安装其中的依赖
|
17 |
RUN if [ -f requirements.txt ]; then \
|
18 |
pip install --no-cache-dir -r requirements.txt; \
|
19 |
fi
|
20 |
|
21 |
+
# 创建数据目录并赋予权限
|
22 |
+
RUN mkdir -p /app/data && chmod -R 777 /app/data
|
23 |
|
24 |
# 创建.env文件
|
25 |
RUN touch .env
|
26 |
|
27 |
+
# 复制备份脚本(稍后单独创建)
|
28 |
+
COPY sync_data.sh /app/sync_data.sh
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
RUN chmod +x /app/sync_data.sh
|
30 |
|
|
|
31 |
EXPOSE 7860
|
32 |
|
33 |
# 使用备份脚本启动服务
|