Thermostatic commited on
Commit
ea8364e
Β·
verified Β·
1 Parent(s): be37a83

Update Dockerfile

Browse files
Files changed (1) hide show
  1. Dockerfile +40 -43
Dockerfile CHANGED
@@ -1,9 +1,7 @@
 
1
  FROM python:3.9-slim
2
 
3
- # Set WORKDIR early, it creates /app owned by root initially.
4
- WORKDIR /app
5
-
6
- # Install system dependencies
7
  RUN apt-get update && apt-get install -y \
8
  build-essential \
9
  curl \
@@ -12,57 +10,56 @@ RUN apt-get update && apt-get install -y \
12
  libgomp1 \
13
  && rm -rf /var/lib/apt/lists/*
14
 
15
- # Create a non-root group first. Using a distinct name like 'appgroup'.
16
- # Using fixed GID 1001. The -r flag makes it a system group.
17
- RUN groupadd -r --gid 1001 appgroup
18
 
19
- # Create a non-root user.
20
- # User 'appuser', UID 1001, primary group 'appgroup'.
21
- # Home directory /app (which already exists from WORKDIR).
22
- # Shell /bin/false (common for non-login service accounts, universally available).
23
- # The -r flag creates a system user.
24
- # We are NOT using -c "comment" here to avoid potential quoting issues.
25
- RUN useradd -r --uid 1001 --gid appgroup --home-dir /app --shell /bin/false appuser
26
 
27
- # Set environment variables
28
- # PYTHONUNBUFFERED: Ensures Python output is sent directly (good for logs)
29
- # STREAMLIT_*: Streamlit specific settings for headless/usage stats
30
- # HOME: Critical for Streamlit and other tools to find user-specific config paths
31
- # HF_HOME: Specifies where Hugging Face libraries should store cache
32
- ENV PYTHONUNBUFFERED=1 \
 
33
  STREAMLIT_SERVER_HEADLESS="true" \
34
  STREAMLIT_BROWSER_GATHERUSAGSTATS="false" \
35
  STREAMLIT_GLOBAL_GATHERUSAGSTATS="false" \
36
- HOME="/app" \
37
- HF_HOME="/app/.hf_cache"
38
 
39
- # Create cache and config directories needed by the application.
40
- # Crucially, change ownership of /app (our HOME and WORKDIR) and its subdirectories
41
- # to the appuser (UID 1001, GID 1001). This is done as root before switching user.
42
- RUN mkdir -p /app/.hf_cache /app/.streamlit \
43
- && chown -R 1001:1001 /app
44
 
45
- # Copy requirements first to leverage Docker cache
46
- COPY requirements.txt ./
47
- # Install Python dependencies (still as root, but will be used by appuser)
48
- RUN pip3 install --no-cache-dir -r requirements.txt
 
49
 
50
- # Copy the application source code.
51
- # Use --chown with numeric UID:GID to ensure appuser owns these files.
52
- # This assumes your streamlit_app.py is inside a 'src' folder in your build context.
53
- COPY --chown=1001:1001 src/ ./src/
54
- # If your streamlit_app.py is at the root of your build context (next to Dockerfile), use:
55
- # COPY --chown=1001:1001 streamlit_app.py ./streamlit_app.py
56
-
57
- # Switch to the non-root user (use the username)
58
- USER appuser
 
 
59
 
60
  # Expose the port Streamlit will run on
61
  EXPOSE 8501
62
 
63
- # Healthcheck
64
  HEALTHCHECK CMD curl --fail http://localhost:8501/_stcore/health
65
 
66
  # Command to run the Streamlit application
67
- # Adjust path if streamlit_app.py was copied to /app/streamlit_app.py
68
- ENTRYPOINT ["streamlit", "run", "src/streamlit_app.py", "--server.port=8501", "--server.address=0.0.0.0"]
 
 
 
 
1
+ # Use a specific Python 3.9 slim image
2
  FROM python:3.9-slim
3
 
4
+ # Install system dependencies (as root, before creating the non-root user)
 
 
 
5
  RUN apt-get update && apt-get install -y \
6
  build-essential \
7
  curl \
 
10
  libgomp1 \
11
  && rm -rf /var/lib/apt/lists/*
12
 
13
+ # Create a non-root user "appuser" with UID 1000 (as expected by HF Spaces)
14
+ # and create its home directory.
15
+ RUN useradd -m -u 1000 appuser
16
 
17
+ # Switch to the non-root user "appuser"
18
+ USER appuser
 
 
 
 
 
19
 
20
+ # Set environment variables for the "appuser"
21
+ # HOME is crucial for tools like pip and streamlit to find user-specific paths
22
+ ENV HOME=/home/appuser
23
+ # Add user's local bin to PATH (where pip --user installs scripts)
24
+ ENV PATH="$HOME/.local/bin:$PATH"
25
+ # Set Hugging Face cache to be within the user's home directory
26
+ ENV HF_HOME="$HOME/.cache/huggingface" \
27
  STREAMLIT_SERVER_HEADLESS="true" \
28
  STREAMLIT_BROWSER_GATHERUSAGSTATS="false" \
29
  STREAMLIT_GLOBAL_GATHERUSAGSTATS="false" \
30
+ PYTHONUNBUFFERED=1
 
31
 
32
+ # Set the working directory inside the user's home
33
+ # All subsequent COPY and RUN commands will be relative to this
34
+ WORKDIR $HOME/app
 
 
35
 
36
+ # Copy requirements.txt first and install dependencies as "appuser"
37
+ # --chown=appuser:appuser ensures the copied file is owned by appuser
38
+ COPY --chown=appuser:appuser requirements.txt ./
39
+ # Pip will install packages to user's site-packages (e.g., $HOME/.local/lib/python3.9/site-packages)
40
+ RUN pip install --no-cache-dir -r requirements.txt
41
 
42
+ # Copy the application source code into the WORKDIR
43
+ # Ensure your streamlit_app.py is in a 'src' folder at the root of your build context
44
+ # e.g., your project structure is:
45
+ # .
46
+ # β”œβ”€β”€ Dockerfile
47
+ # β”œβ”€β”€ requirements.txt
48
+ # └── src/
49
+ # └── streamlit_app.py
50
+ COPY --chown=appuser:appuser src/ ./src/
51
+ # If your streamlit_app.py is at the root of your project (next to Dockerfile):
52
+ # COPY --chown=appuser:appuser streamlit_app.py ./streamlit_app.py
53
 
54
  # Expose the port Streamlit will run on
55
  EXPOSE 8501
56
 
57
+ # Healthcheck (Streamlit's health endpoint)
58
  HEALTHCHECK CMD curl --fail http://localhost:8501/_stcore/health
59
 
60
  # Command to run the Streamlit application
61
+ # Ensure the path to your script is correct relative to WORKDIR ($HOME/app)
62
+ # If streamlit_app.py is in $HOME/app/src/streamlit_app.py:
63
+ ENTRYPOINT ["streamlit", "run", "src/streamlit_app.py", "--server.port=8501", "--server.address=0.0.0.0"]
64
+ # If streamlit_app.py is in $HOME/app/streamlit_app.py (if you changed the COPY above):
65
+ # ENTRYPOINT ["streamlit", "run", "streamlit_app.py", "--server.port=8501", "--server.address=0.0.0.0"]