Spaces:
Runtime error
Runtime error
Update Dockerfile
Browse files- Dockerfile +68 -37
Dockerfile
CHANGED
|
@@ -1,57 +1,88 @@
|
|
| 1 |
-
# Use
|
| 2 |
-
FROM python:3.9-slim-buster@sha256:
|
| 3 |
|
| 4 |
-
#
|
|
|
|
| 5 |
ENV PYTHONDONTWRITEBYTECODE=1 \
|
| 6 |
PYTHONUNBUFFERED=1 \
|
|
|
|
|
|
|
| 7 |
HF_HOME=/app/cache \
|
| 8 |
-
|
| 9 |
-
PIP_NO_CACHE_DIR=1
|
|
|
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
|
| 15 |
-
RUN mkdir -p /app/data /app/cache && \
|
| 16 |
-
useradd -m myuser && \
|
| 17 |
-
chown -R myuser:myuser /app && \
|
| 18 |
-
chmod -R u+rw /app/data /app/cache
|
| 19 |
|
| 20 |
-
#
|
|
|
|
| 21 |
RUN apt-get update && apt-get install -y --no-install-recommends \
|
| 22 |
build-essential \
|
| 23 |
-
|
| 24 |
-
g++ \
|
| 25 |
&& rm -rf /var/lib/apt/lists/*
|
| 26 |
|
| 27 |
-
#
|
| 28 |
-
RUN pip install --upgrade pip
|
| 29 |
-
|
| 30 |
-
# Copy requirements first for better caching
|
| 31 |
COPY requirements.txt .
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
|
|
|
| 35 |
|
| 36 |
-
#
|
| 37 |
-
|
| 38 |
-
RUN python -c "
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
#
|
| 44 |
-
|
|
|
|
| 45 |
|
| 46 |
-
#
|
| 47 |
-
|
| 48 |
-
|
| 49 |
|
| 50 |
-
#
|
| 51 |
-
|
|
|
|
|
|
|
| 52 |
|
| 53 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
EXPOSE 8000
|
| 55 |
|
| 56 |
-
#
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Use official Python 3.9 slim image with explicit hash for security
|
| 2 |
+
FROM python:3.9-slim-buster@sha256:3207a4250aba4249f458872adecf92eea88dc6abd2d76dc5c0f01cac9b53990
|
| 3 |
|
| 4 |
+
# ========== SYSTEM CONFIGURATION ==========
|
| 5 |
+
# Set critical environment variables
|
| 6 |
ENV PYTHONDONTWRITEBYTECODE=1 \
|
| 7 |
PYTHONUNBUFFERED=1 \
|
| 8 |
+
PYTHONMALLOC=malloc \
|
| 9 |
+
TOKENIZERS_PARALLELISM=false \
|
| 10 |
HF_HOME=/app/cache \
|
| 11 |
+
HUGGINGFACE_HUB_CACHE=/app/cache \
|
| 12 |
+
PIP_NO_CACHE_DIR=1 \
|
| 13 |
+
GUNICORN_CMD_ARGS="--timeout 300 --workers 2 --preload"
|
| 14 |
|
| 15 |
+
# Create directory structure with proper permissions
|
| 16 |
+
RUN mkdir -p /app/{data,cache,config} && \
|
| 17 |
+
useradd -m -u 1001 appuser && \
|
| 18 |
+
chown -R appuser:appuser /app && \
|
| 19 |
+
chmod -R 755 /app
|
| 20 |
|
| 21 |
+
WORKDIR /app
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
+
# ========== DEPENDENCY INSTALLATION ==========
|
| 24 |
+
# Install system dependencies in one layer
|
| 25 |
RUN apt-get update && apt-get install -y --no-install-recommends \
|
| 26 |
build-essential \
|
| 27 |
+
libgomp1 \
|
|
|
|
| 28 |
&& rm -rf /var/lib/apt/lists/*
|
| 29 |
|
| 30 |
+
# Install Python dependencies with version pinning
|
|
|
|
|
|
|
|
|
|
| 31 |
COPY requirements.txt .
|
| 32 |
|
| 33 |
+
RUN pip install --upgrade pip && \
|
| 34 |
+
pip install --no-cache-dir -r requirements.txt && \
|
| 35 |
+
pip cache purge
|
| 36 |
|
| 37 |
+
# ========== MODEL PRELOADING ==========
|
| 38 |
+
# Pre-download models with error handling
|
| 39 |
+
RUN python -c "\
|
| 40 |
+
try: \
|
| 41 |
+
from sentence_transformers import SentenceTransformer; \
|
| 42 |
+
SentenceTransformer('all-MiniLM-L6-v2', cache_folder='/app/cache'); \
|
| 43 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer; \
|
| 44 |
+
AutoModelForCausalLM.from_pretrained('distilgpt2', cache_dir='/app/cache'); \
|
| 45 |
+
AutoTokenizer.from_pretrained('distilgpt2', cache_dir='/app/cache'); \
|
| 46 |
+
except Exception as e: \
|
| 47 |
+
print(f'Model preloading failed: {str(e)}'); \
|
| 48 |
+
raise \
|
| 49 |
+
"
|
| 50 |
|
| 51 |
+
# Verify model cache integrity
|
| 52 |
+
RUN find /app/cache -type f -exec ls -lh {} \+ && \
|
| 53 |
+
du -sh /app/cache
|
| 54 |
|
| 55 |
+
# ========== APPLICATION SETUP ==========
|
| 56 |
+
# Copy application files (excluding .dockerignore patterns)
|
| 57 |
+
COPY --chown=appuser:appuser . .
|
| 58 |
|
| 59 |
+
# Ensure proper file permissions
|
| 60 |
+
RUN chmod a+x /app/entrypoint.sh && \
|
| 61 |
+
chmod -R a+r /app && \
|
| 62 |
+
find /app -type d -exec chmod a+rx {} \+
|
| 63 |
|
| 64 |
+
# Switch to non-root user
|
| 65 |
+
USER appuser
|
| 66 |
+
|
| 67 |
+
# Health check (every 30s, 5s timeout, 3 retries)
|
| 68 |
+
HEALTHCHECK --interval=30s --timeout=5s --retries=3 \
|
| 69 |
+
CMD curl -f http://localhost:8000/health || exit 1
|
| 70 |
+
|
| 71 |
+
# Expose port
|
| 72 |
EXPOSE 8000
|
| 73 |
|
| 74 |
+
# ========== RUNTIME CONFIGURATION ==========
|
| 75 |
+
# Entrypoint script for pre-launch checks
|
| 76 |
+
COPY entrypoint.sh .
|
| 77 |
+
ENTRYPOINT ["./entrypoint.sh"]
|
| 78 |
+
|
| 79 |
+
# Start Gunicorn with optimized settings
|
| 80 |
+
CMD ["gunicorn", "--bind", "0.0.0.0:8000", \
|
| 81 |
+
"--workers", "2", \
|
| 82 |
+
"--timeout", "300", \
|
| 83 |
+
"--preload", \
|
| 84 |
+
"--worker-class", "sync", \
|
| 85 |
+
"--access-logfile", "-", \
|
| 86 |
+
"--error-logfile", "-", \
|
| 87 |
+
"--log-level", "info", \
|
| 88 |
+
"app:app"]
|