You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于Docker的FastAPI HF Spaces应用无法为多用户创建实例

Hugging Face Spaces部署FastAPI多用户独立实例问题

我在Hugging Face Spaces环境部署FastAPI应用时,遇到所有用户共用一个实例的问题。试过通过Docker镜像和entrypoint.sh脚本处理用户专属配置,但目前的设置没法给每个进入空间的用户动态创建独立实例。现在需要修改Dockerfile或entrypoint.sh的方案,实现多用户拥有独立FastAPI应用实例的需求。

我已经尝试过多种策略,包括把大部分用户设置逻辑放在entrypoint.sh脚本里,也测试过Dockerfile的不同配置,但都没成功。

当前Dockerfile内容

# Set the working directory to /app
WORKDIR /app

# Copy the current directory contents into the container at /app
COPY . /app

# Define the user ID in the environment variable USER_ID with a default value
ARG USER_ID=1000
ENV USER_ID=$USER_ID

# Check if the user already exists
RUN if [ -z "$USER_ID" ]; then \
      echo "User ID not provided. Using the default user ID 1000."; \
      USER_ID=1000; \
    fi && \
    if id "$USER_ID" >/dev/null 2>&1; then \
      echo "User with ID $USER_ID already exists."; \
    else \
      adduser --uid "$USER_ID" --disabled-password --gecos '' appuser; \
    fi

# Set appropriate permissions for the application directory
RUN chown -R appuser:appuser /app && chmod -R 755 /app

# Install gosu (adjust the package manager based on your base image)
RUN apt-get update && apt-get install -y gosu && rm -rf /var/lib/apt/lists/*

# Set the entrypoint script as executable
COPY entrypoint.sh /usr/local/bin/entrypoint.sh
RUN chmod +x /usr/local/bin/entrypoint.sh

# Switch to the user for improved security
USER appuser

# Define the entrypoint script to handle user creation and application startup
ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]

# Default command to run if the user doesn't provide a command
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "7860", "--reload"]

当前entrypoint.sh脚本内容

#!/bin/sh
set -e

# Set permissions at runtime
chmod -R 777 /app

# Execute the main command
exec "$@"

内容的提问来源于stack exchange,提问作者Circulartextapps

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 01:24:53