Spaces:
Runtime error
Runtime error
Update Dockerfile
Browse files- Dockerfile +27 -18
Dockerfile
CHANGED
|
@@ -5,7 +5,7 @@ FROM python:3.10-slim
|
|
| 5 |
ENV DEBIAN_FRONTEND=noninteractive
|
| 6 |
|
| 7 |
# Install system dependencies
|
| 8 |
-
# - Essential for Pillow
|
| 9 |
# - Font handling: fontconfig
|
| 10 |
# - Installs Microsoft Core Fonts (including Arial): ttf-mscorefonts-installer
|
| 11 |
RUN apt-get update && \
|
|
@@ -16,48 +16,57 @@ RUN apt-get update && \
|
|
| 16 |
# Accept the EULA for ttf-mscorefonts-installer non-interactively
|
| 17 |
&& echo "ttf-mscorefonts-installer msttcorefonts/accepted-mscorefonts-eula select true" | debconf-set-selections \
|
| 18 |
&& apt-get install -y --no-install-recommends ttf-mscorefonts-installer \
|
|
|
|
| 19 |
&& apt-get clean \
|
| 20 |
&& rm -rf /var/lib/apt/lists/* \
|
| 21 |
-
# Update font cache to make installed fonts available
|
| 22 |
&& fc-cache -f -v
|
| 23 |
|
| 24 |
-
# Set the working directory in the container
|
| 25 |
WORKDIR /app
|
| 26 |
|
| 27 |
-
# Create a non-root user and group for better security
|
| 28 |
-
# Using
|
| 29 |
RUN groupadd -g 1000 appuser && useradd --no-log-init -u 1000 -g appuser appuser
|
| 30 |
|
| 31 |
-
# Create necessary application directories
|
| 32 |
-
#
|
|
|
|
|
|
|
| 33 |
RUN mkdir -p /app/templates /app/generated_images && \
|
| 34 |
chown -R appuser:appuser /app
|
| 35 |
|
| 36 |
-
# Copy the requirements file first
|
| 37 |
-
# Ensure
|
| 38 |
COPY --chown=appuser:appuser requirements.txt .
|
| 39 |
|
| 40 |
-
# Install Python dependencies specified in requirements.txt
|
|
|
|
| 41 |
RUN pip install --no-cache-dir -r requirements.txt
|
| 42 |
|
| 43 |
-
# Copy the rest of the application files into the container
|
| 44 |
-
# Ensure '
|
| 45 |
COPY --chown=appuser:appuser app.py .
|
| 46 |
|
| 47 |
# ---- IMPORTANT ----
|
| 48 |
-
# The following line requires 'arial.ttf' to be present in the root of your repository
|
|
|
|
| 49 |
COPY --chown=appuser:appuser arial.ttf .
|
| 50 |
# ---- IMPORTANT ----
|
| 51 |
|
| 52 |
-
# Copy the entire templates directory
|
|
|
|
|
|
|
| 53 |
COPY --chown=appuser:appuser templates ./templates
|
| 54 |
|
| 55 |
-
# Switch to the non-root user
|
|
|
|
| 56 |
USER appuser
|
| 57 |
|
| 58 |
-
# Define environment variable placeholder
|
| 59 |
-
#
|
| 60 |
ENV TELEGRAM_TOKEN=""
|
| 61 |
|
| 62 |
-
# Command to run the application when the container launches
|
|
|
|
| 63 |
CMD ["python", "app.py"]
|
|
|
|
| 5 |
ENV DEBIAN_FRONTEND=noninteractive
|
| 6 |
|
| 7 |
# Install system dependencies
|
| 8 |
+
# - Essential for Pillow image processing: libgl1-mesa-glx, libglib2.0-0
|
| 9 |
# - Font handling: fontconfig
|
| 10 |
# - Installs Microsoft Core Fonts (including Arial): ttf-mscorefonts-installer
|
| 11 |
RUN apt-get update && \
|
|
|
|
| 16 |
# Accept the EULA for ttf-mscorefonts-installer non-interactively
|
| 17 |
&& echo "ttf-mscorefonts-installer msttcorefonts/accepted-mscorefonts-eula select true" | debconf-set-selections \
|
| 18 |
&& apt-get install -y --no-install-recommends ttf-mscorefonts-installer \
|
| 19 |
+
# Clean up apt caches to reduce image size
|
| 20 |
&& apt-get clean \
|
| 21 |
&& rm -rf /var/lib/apt/lists/* \
|
| 22 |
+
# Update font cache to make installed fonts available to applications like Pillow
|
| 23 |
&& fc-cache -f -v
|
| 24 |
|
| 25 |
+
# Set the working directory in the container for subsequent commands
|
| 26 |
WORKDIR /app
|
| 27 |
|
| 28 |
+
# Create a non-root user and group for better security.
|
| 29 |
+
# Using UID/GID 1000 is common for Hugging Face Spaces.
|
| 30 |
RUN groupadd -g 1000 appuser && useradd --no-log-init -u 1000 -g appuser appuser
|
| 31 |
|
| 32 |
+
# Create necessary application directories within /app.
|
| 33 |
+
# Then, set ownership of the entire /app directory (and its contents)
|
| 34 |
+
# to the 'appuser'. This ensures the application, running as 'appuser',
|
| 35 |
+
# has the necessary permissions, especially for /app/generated_images.
|
| 36 |
RUN mkdir -p /app/templates /app/generated_images && \
|
| 37 |
chown -R appuser:appuser /app
|
| 38 |
|
| 39 |
+
# Copy the requirements file first to leverage Docker layer caching.
|
| 40 |
+
# Ensure 'appuser' owns this copied file.
|
| 41 |
COPY --chown=appuser:appuser requirements.txt .
|
| 42 |
|
| 43 |
+
# Install Python dependencies specified in requirements.txt.
|
| 44 |
+
# --no-cache-dir reduces image size by not storing the pip cache.
|
| 45 |
RUN pip install --no-cache-dir -r requirements.txt
|
| 46 |
|
| 47 |
+
# Copy the rest of the application files into the container's /app directory.
|
| 48 |
+
# Ensure 'appuser' owns these copied files.
|
| 49 |
COPY --chown=appuser:appuser app.py .
|
| 50 |
|
| 51 |
# ---- IMPORTANT ----
|
| 52 |
+
# The following line requires 'arial.ttf' to be present in the root of your repository.
|
| 53 |
+
# This file will be copied to /app/arial.ttf inside the container.
|
| 54 |
COPY --chown=appuser:appuser arial.ttf .
|
| 55 |
# ---- IMPORTANT ----
|
| 56 |
|
| 57 |
+
# Copy the entire 'templates' directory (and its contents) from your repository root
|
| 58 |
+
# to /app/templates inside the container.
|
| 59 |
+
# This requires the 'templates' directory to exist in your repository root.
|
| 60 |
COPY --chown=appuser:appuser templates ./templates
|
| 61 |
|
| 62 |
+
# Switch to the non-root user 'appuser' for running the application.
|
| 63 |
+
# Subsequent commands (like CMD) will run as this user.
|
| 64 |
USER appuser
|
| 65 |
|
| 66 |
+
# Define an environment variable placeholder for the Telegram token.
|
| 67 |
+
# The actual token MUST be set in your Hugging Face Space's secrets settings.
|
| 68 |
ENV TELEGRAM_TOKEN=""
|
| 69 |
|
| 70 |
+
# Command to run the application when the container launches.
|
| 71 |
+
# This executes 'python app.py' as the 'appuser'.
|
| 72 |
CMD ["python", "app.py"]
|