DavidFernandes commited on
Commit
ad53b25
·
1 Parent(s): b73dbf7

Fix Dockerfile: Install dependencies as root to avoid permission errors

Browse files
Files changed (1) hide show
  1. Dockerfile +23 -15
Dockerfile CHANGED
@@ -1,10 +1,10 @@
1
  FROM python:3.9
2
 
3
- # Set working directory to the user's home/app
4
- WORKDIR /home/user/app
5
 
6
- # Install system dependencies for opencv and ffmpeg
7
- # We do this as root before switching users
8
  USER root
9
  RUN apt-get update && apt-get install -y \
10
  ffmpeg \
@@ -12,25 +12,33 @@ RUN apt-get update && apt-get install -y \
12
  libxext6 \
13
  && rm -rf /var/lib/apt/lists/*
14
 
15
- # Create a non-root user (required by HF Spaces)
 
 
 
 
 
 
 
16
  RUN useradd -m -u 1000 user
17
 
18
- # Switch to the new user
19
  USER user
20
- ENV PATH="/home/user/.local/bin:$PATH"
 
21
 
22
- # Copy the current directory contents into the container at /home/user/app
23
- # using --chown to ensure the user owns the files
24
- COPY --chown=user . .
25
 
26
- # Install python dependencies
27
- RUN pip install --no-cache-dir --upgrade -r requirements.txt
 
28
 
29
- # Download models during build so they are cached in the image
30
- # This will save them to /home/user/.u2net
31
  RUN python download_models.py
32
 
33
- # Expose the port (Hugging Face Spaces defaults to 7860)
34
  EXPOSE 7860
35
 
36
  # Command to run the application
 
1
  FROM python:3.9
2
 
3
+ # Workdir for build steps
4
+ WORKDIR /code
5
 
6
+ # Install system dependencies (Run as ROOT)
7
+ # This ensures we have permission to install system packages
8
  USER root
9
  RUN apt-get update && apt-get install -y \
10
  ffmpeg \
 
12
  libxext6 \
13
  && rm -rf /var/lib/apt/lists/*
14
 
15
+ # Copy requirements file
16
+ COPY requirements.txt /code/requirements.txt
17
+
18
+ # Install Python dependencies globally (Run as ROOT)
19
+ # Installing as root avoids permission issues with /home/user/.local
20
+ RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
21
+
22
+ # Create a non-root user (Required by Hugging Face Spaces)
23
  RUN useradd -m -u 1000 user
24
 
25
+ # Switch to the non-root user for the application runtime
26
  USER user
27
+ ENV HOME=/home/user \
28
+ PATH=/home/user/.local/bin:$PATH
29
 
30
+ # Set working directory to the user's home/app
31
+ WORKDIR $HOME/app
 
32
 
33
+ # Copy the application code (Run as USER)
34
+ # We change ownership to the user so they can read/write if needed
35
+ COPY --chown=user . $HOME/app
36
 
37
+ # Download models (Run as USER)
38
+ # This ensures models are saved to /home/user/.u2net, which the app can read
39
  RUN python download_models.py
40
 
41
+ # Expose the application port
42
  EXPOSE 7860
43
 
44
  # Command to run the application