George Yang commited on
Commit
24f75b0
·
1 Parent(s): 36ed1cd

Fix: Add Hugging Face Spaces user requirements

Browse files

- Add user creation with UID 1000 (required by HF Spaces)
- Set proper HOME and PATH environment variables
- Copy files with correct ownership (--chown=user)
- Run application as non-root user

Follows Hugging Face Docker Spaces best practices:
https://huggingface.co/docs/hub/spaces-sdks-docker

Files changed (1) hide show
  1. Dockerfile +17 -10
Dockerfile CHANGED
@@ -3,28 +3,39 @@
3
 
4
  FROM python:3.12-slim
5
 
6
- # Set working directory
7
- WORKDIR /app
8
-
9
  # Set environment variables
10
  ENV PYTHONUNBUFFERED=1 \
11
  PYTHONDONTWRITEBYTECODE=1 \
12
  PORT=7860
13
 
14
- # Install system dependencies
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  RUN apt-get update && \
16
  apt-get install -y --no-install-recommends \
17
  gcc \
18
  && rm -rf /var/lib/apt/lists/*
 
19
 
20
  # Copy requirements first for better Docker layer caching
21
- COPY requirements.txt .
22
 
23
  # Install Python dependencies
24
  RUN pip install --no-cache-dir -r requirements.txt
25
 
26
  # Copy project files
27
- COPY . .
28
 
29
  # Install the package in editable mode
30
  RUN pip install --no-cache-dir -e .
@@ -32,9 +43,5 @@ RUN pip install --no-cache-dir -e .
32
  # Expose Hugging Face Spaces default port
33
  EXPOSE 7860
34
 
35
- # Health check endpoint
36
- HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
37
- CMD python -c "import urllib.request; urllib.request.urlopen('http://localhost:7860/').read()"
38
-
39
  # Run the FastAPI application with uvicorn
40
  CMD ["uvicorn", "web.app:app", "--host", "0.0.0.0", "--port", "7860"]
 
3
 
4
  FROM python:3.12-slim
5
 
 
 
 
6
  # Set environment variables
7
  ENV PYTHONUNBUFFERED=1 \
8
  PYTHONDONTWRITEBYTECODE=1 \
9
  PORT=7860
10
 
11
+ # The two following lines are required for Hugging Face Spaces Dev Mode
12
+ # See: https://huggingface.co/docs/hub/spaces-sdks-docker
13
+ RUN useradd -m -u 1000 user
14
+ USER user
15
+
16
+ # Set home to the user's home directory
17
+ ENV HOME=/home/user \
18
+ PATH=/home/user/.local/bin:$PATH
19
+
20
+ # Set working directory
21
+ WORKDIR /app
22
+
23
+ # Install system dependencies (as root, then switch back to user)
24
+ USER root
25
  RUN apt-get update && \
26
  apt-get install -y --no-install-recommends \
27
  gcc \
28
  && rm -rf /var/lib/apt/lists/*
29
+ USER user
30
 
31
  # Copy requirements first for better Docker layer caching
32
+ COPY --chown=user requirements.txt .
33
 
34
  # Install Python dependencies
35
  RUN pip install --no-cache-dir -r requirements.txt
36
 
37
  # Copy project files
38
+ COPY --chown=user . .
39
 
40
  # Install the package in editable mode
41
  RUN pip install --no-cache-dir -e .
 
43
  # Expose Hugging Face Spaces default port
44
  EXPOSE 7860
45
 
 
 
 
 
46
  # Run the FastAPI application with uvicorn
47
  CMD ["uvicorn", "web.app:app", "--host", "0.0.0.0", "--port", "7860"]