Spaces:
Sleeping
Sleeping
File size: 1,857 Bytes
bdf1d27 46ab407 bdf1d27 46ab407 bdf1d27 323bcb7 bdf1d27 46ab407 bdf1d27 323bcb7 bdf1d27 46ab407 bdf1d27 323bcb7 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | # Use Python 3.11 slim as base image
FROM python:3.11-slim AS builder
# Set working directory
WORKDIR /app
# Install system dependencies required for OpenCV and other packages
RUN apt-get update && apt-get install -y \
build-essential \
libgl1-mesa-glx \
libglib2.0-0 \
libsm6 \
libxext6 \
libxrender-dev \
cmake \
&& rm -rf /var/lib/apt/lists/*
# Create and activate virtual environment
RUN python -m venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"
# Install Python dependencies
COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt
# Disable numba caching by setting an environment variable
RUN echo "NUMBA_CACHE_DIR=/tmp" >> /etc/environment
# Final stage
FROM python:3.11-slim
# Copy virtual environment from builder
COPY --from=builder /opt/venv /opt/venv
# Set environment variables
ENV PATH="/opt/venv/bin:$PATH" \
PYTHONUNBUFFERED=1 \
GRADIO_SERVER_NAME=0.0.0.0 \
GRADIO_SERVER_PORT=7860 \
NUMBA_CACHE_DIR=/tmp \
U2NET_HOME=/app/.u2net \
REMBG_CACHE_DIR=/app/.u2net
# Install runtime system dependencies
RUN apt-get update && apt-get install -y \
libgl1-mesa-glx \
libglib2.0-0 \
libsm6 \
libxext6 \
libxrender-dev \
&& rm -rf /var/lib/apt/lists/*
# Set working directory
WORKDIR /app
# Copy application code
COPY . ./
# Create necessary directories with appropriate permissions
RUN mkdir -p storage/uploads storage/processed && \
mkdir -p /app/.u2net && \
chmod 777 /app/.u2net storage/uploads storage/processed
# Create a non-root user
RUN useradd -m -u 1000 appuser && \
chown -R appuser:appuser /app
# Switch to non-root user
USER appuser
# Ensure required Python imports are pre-compiled (optional for optimization)
RUN python -m compileall .
# Expose port
EXPOSE 7860
# Set default command
CMD ["python", "app.py"] |