基于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
相关产品推荐
相关产品推荐

