ayush2917 commited on
Commit
2f0747a
·
verified ·
1 Parent(s): ecd483b

Update Dockerfile

Browse files
Files changed (1) hide show
  1. Dockerfile +68 -37
Dockerfile CHANGED
@@ -1,57 +1,88 @@
1
- # Use a slim Python 3.9 base image
2
- FROM python:3.9-slim-buster@sha256:320a7a4250aba4249f458872adecf92eea88dc6abd2d76dc5c0f01cac9b53990
3
 
4
- # Set environment variables
 
5
  ENV PYTHONDONTWRITEBYTECODE=1 \
6
  PYTHONUNBUFFERED=1 \
 
 
7
  HF_HOME=/app/cache \
8
- HUGGINGFACE_HUB_DISABLE_XET=1 \
9
- PIP_NO_CACHE_DIR=1
 
10
 
11
- # Set working directory
12
- WORKDIR /app
 
 
 
13
 
14
- # Create directories with correct permissions
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
- # Install system dependencies
 
21
  RUN apt-get update && apt-get install -y --no-install-recommends \
22
  build-essential \
23
- gcc \
24
- g++ \
25
  && rm -rf /var/lib/apt/lists/*
26
 
27
- # Upgrade pip to the latest version
28
- RUN pip install --upgrade pip
29
-
30
- # Copy requirements first for better caching
31
  COPY requirements.txt .
32
 
33
- # Install dependencies from requirements.txt
34
- RUN pip install --no-cache-dir -r requirements.txt || { echo "Failed to install requirements"; exit 1; }
 
35
 
36
- # Pre-download models
37
- RUN python -c "from sentence_transformers import SentenceTransformer; SentenceTransformer('all-MiniLM-L6-v2', cache_folder='/app/cache')" || { echo "Failed to pre-download SentenceTransformer model"; exit 1; }
38
- RUN python -c "from transformers import AutoModelForCausalLM, AutoTokenizer; AutoModelForCausalLM.from_pretrained('distilgpt2', cache_dir='/app/cache'); AutoTokenizer.from_pretrained('distilgpt2', cache_dir='/app/cache')" || { echo "Failed to pre-download transformer model"; exit 1; }
39
- # Verify cache contents
40
- RUN ls -l /app/cache && \
41
- find /app/cache -type f || { echo "Cache verification failed"; exit 1; }
 
 
 
 
 
 
 
42
 
43
- # Copy all project files
44
- COPY . .
 
45
 
46
- # Ensure permissions after copy
47
- RUN chown -R myuser:myuser /app && \
48
- chmod -R u+rw /app/data /app/cache
49
 
50
- # Set non-root user
51
- USER myuser
 
 
52
 
53
- # Expose port for Gunicorn
 
 
 
 
 
 
 
54
  EXPOSE 8000
55
 
56
- # Start Gunicorn with a single worker to reduce memory usage
57
- CMD ["gunicorn", "--bind", "0.0.0.0:8000", "--workers", "1", "--timeout", "120", "app:app"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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"]