Update Dockerfile
Browse files- Dockerfile +40 -43
Dockerfile
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
|
|
| 1 |
FROM python:3.9-slim
|
| 2 |
|
| 3 |
-
#
|
| 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
|
| 16 |
-
#
|
| 17 |
-
RUN
|
| 18 |
|
| 19 |
-
#
|
| 20 |
-
|
| 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 |
-
#
|
| 29 |
-
|
| 30 |
-
#
|
| 31 |
-
|
| 32 |
-
|
|
|
|
| 33 |
STREAMLIT_SERVER_HEADLESS="true" \
|
| 34 |
STREAMLIT_BROWSER_GATHERUSAGSTATS="false" \
|
| 35 |
STREAMLIT_GLOBAL_GATHERUSAGSTATS="false" \
|
| 36 |
-
|
| 37 |
-
HF_HOME="/app/.hf_cache"
|
| 38 |
|
| 39 |
-
#
|
| 40 |
-
#
|
| 41 |
-
|
| 42 |
-
RUN mkdir -p /app/.hf_cache /app/.streamlit \
|
| 43 |
-
&& chown -R 1001:1001 /app
|
| 44 |
|
| 45 |
-
# Copy requirements first
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
|
|
|
| 49 |
|
| 50 |
-
# Copy the application source code
|
| 51 |
-
#
|
| 52 |
-
#
|
| 53 |
-
|
| 54 |
-
#
|
| 55 |
-
#
|
| 56 |
-
|
| 57 |
-
#
|
| 58 |
-
|
|
|
|
|
|
|
| 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 |
-
#
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 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"]
|