cloudpasteview / Dockerfile
dragg2's picture
Update Dockerfile
04e49a6 verified
raw
history blame
5.37 kB
FROM ymlisoft/kkfileview
USER root
# 1. 安装必要依赖
RUN apt-get update && apt-get install -y \
nginx \
apache2-utils \
xvfb \
curl \
libfreetype6 \
fontconfig \
fonts-wqy-zenhei \
&& rm -rf /var/lib/apt/lists/* \
&& fc-cache -fv
# 2. 环境变量配置
ENV SERVER_PORT=8012
ENV KKFILEVIEW_SECURITY_TRUST_HOST=default
ENV AUTH_USERNAME=admin
ENV AUTH_PASSWORD=yourpassword
# 3. JVM配置
# - 堆内存: 4GB
# - G1GC: 低延迟垃圾回收
# - 元空间: 256MB
ENV JAVA_OPTS="-server \
-Xms4g \
-Xmx4g \
-XX:+UseG1GC \
-XX:MaxGCPauseMillis=200 \
-XX:MetaspaceSize=256m \
-XX:MaxMetaspaceSize=512m \
-Djava.awt.headless=true \
-Dsun.java2d.fontpath=/usr/share/fonts \
-Dfile.encoding=UTF-8 \
-Dserver.tomcat.threads.max=16 \
-Dserver.tomcat.threads.min-spare=4 \
-Dserver.tomcat.accept-count=50 \
-Dserver.tomcat.max-connections=200"
# 4. Nginx 配置
RUN rm -rf /etc/nginx/sites-enabled/* /etc/nginx/conf.d/* /etc/nginx/nginx.conf && \
printf 'worker_processes 2;\n\
worker_rlimit_nofile 8192;\n\
error_log /dev/stderr warn;\n\
pid /var/run/nginx.pid;\n\
\n\
events {\n\
worker_connections 2048;\n\
multi_accept on;\n\
use epoll;\n\
}\n\
\n\
http {\n\
include /etc/nginx/mime.types;\n\
default_type application/octet-stream;\n\
\n\
log_format main '"'"'$remote_addr - $remote_user [$time_local] "$request" '"'"'\n\
'"'"'$status $body_bytes_sent "$http_referer" '"'"'\n\
'"'"'"$http_user_agent" rt=$request_time'"'"';\n\
\n\
access_log /dev/stdout main;\n\
\n\
sendfile on;\n\
tcp_nopush on;\n\
tcp_nodelay on;\n\
keepalive_timeout 65;\n\
keepalive_requests 100;\n\
\n\
client_max_body_size 500M;\n\
client_body_buffer_size 1M;\n\
client_body_timeout 300s;\n\
\n\
proxy_buffering on;\n\
proxy_buffer_size 8k;\n\
proxy_buffers 4 16k;\n\
proxy_connect_timeout 60s;\n\
proxy_send_timeout 300s;\n\
proxy_read_timeout 300s;\n\
\n\
gzip on;\n\
gzip_vary on;\n\
gzip_min_length 1024;\n\
gzip_comp_level 5;\n\
gzip_types text/plain text/css text/xml text/javascript application/json application/javascript;\n\
\n\
upstream kkfileview {\n\
server 127.0.0.1:8012;\n\
keepalive 16;\n\
keepalive_requests 100;\n\
keepalive_timeout 60s;\n\
}\n\
\n\
server {\n\
listen 7860 default_server;\n\
server_name _;\n\
\n\
auth_basic "Restricted Access";\n\
auth_basic_user_file /etc/nginx/.htpasswd;\n\
\n\
location / {\n\
proxy_pass http://kkfileview;\n\
proxy_http_version 1.1;\n\
proxy_set_header Connection "";\n\
proxy_set_header Host $http_host;\n\
proxy_set_header X-Real-IP $remote_addr;\n\
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;\n\
proxy_set_header X-Forwarded-Proto $http_x_forwarded_proto;\n\
\n\
proxy_buffering on;\n\
proxy_buffer_size 8k;\n\
proxy_buffers 4 16k;\n\
proxy_read_timeout 300s;\n\
}\n\
\n\
location /health {\n\
auth_basic off;\n\
return 200 "OK";\n\
add_header Content-Type text/plain;\n\
}\n\
}\n\
}\n' > /etc/nginx/nginx.conf
# 5. 启动脚本
RUN printf '#!/bin/bash\n\
set -e\n\
\n\
echo "=== Starting KKFileView (Tuned) for HuggingFace Spaces ==="\n\
\n\
# 查找 KKFileView 启动脚本\n\
STARTUP_SCRIPT=$(find /opt -name "startup.sh" -o -name "kkFileView" | grep "bin/" | head -n 1)\n\
KK_ROOT_DIR=$(dirname $(dirname "$STARTUP_SCRIPT"))\n\
\n\
# 生成 htpasswd 文件\n\
htpasswd -cb /etc/nginx/.htpasswd "$AUTH_USERNAME" "$AUTH_PASSWORD"\n\
\n\
# 启动 Xvfb\n\
echo "Starting Xvfb..."\n\
Xvfb :99 -screen 0 1024x768x24 >/dev/null 2>&1 &\n\
export DISPLAY=:99\n\
sleep 2\n\
\n\
# 启动 KKFileView\n\
echo "Starting KKFileView on port 8012..."\n\
export JAVA_OPTS="$JAVA_OPTS"\n\
\n\
if [[ "$STARTUP_SCRIPT" == *".sh" ]]; then\n\
bash "$STARTUP_SCRIPT" 2>&1 | tee /var/log/kkfileview.log &\n\
else\n\
"$STARTUP_SCRIPT" --server.port=8012 2>&1 | tee /var/log/kkfileview.log &\n\
fi\n\
\n\
# 等待日志文件生成\n\
echo "Waiting for log file..."\n\
LOG_FILE="$KK_ROOT_DIR/log/kkFileView.log"\n\
for i in {1..30}; do\n\
if [ -f "$LOG_FILE" ]; then\n\
echo "✓ Found log file: $LOG_FILE"\n\
tail -f "$LOG_FILE" &\n\
break\n\
fi\n\
sleep 1\n\
done\n\
\n\
# 等待 KKFileView 启动\n\
echo "Waiting for KKFileView to start..."\n\
for i in {1..40}; do\n\
if curl -s http://127.0.0.1:8012 >/dev/null 2>&1; then\n\
echo "✓ KKFileView is ready on port 8012!"\n\
break\n\
fi\n\
if [ $i -eq 40 ]; then\n\
echo "ERROR: KKFileView failed to start within 80 seconds"\n\
exit 1\n\
fi\n\
echo "Waiting... ($i/40)"\n\
sleep 2\n\
done\n\
\n\
# 启动 Nginx\n\
echo "Starting Nginx on port 7860..."\n\
nginx -g "daemon off;"\n' > /start.sh && \
chmod +x /start.sh
# 6. 健康检查
HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \
CMD curl -f http://localhost:7860/health || exit 1
# 7. 暴露端口
EXPOSE 7860
# 8. 启动命令
ENTRYPOINT []
CMD ["/start.sh"]