tommytracx commited on
Commit
5cd2388
·
verified ·
1 Parent(s): a45c90b

Update Dockerfile

Browse files
Files changed (1) hide show
  1. Dockerfile +74 -8
Dockerfile CHANGED
@@ -1,11 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  FROM python:3.11-slim
2
 
 
 
 
 
 
3
  WORKDIR /app
4
 
5
- # Install system dependencies
6
- RUN apt-get update && apt-get install -y \
7
- curl \
8
- && rm -rf /var/lib/apt/lists/*
 
 
9
 
10
  # Copy requirements and install Python dependencies
11
  COPY requirements.txt .
@@ -14,12 +42,50 @@ RUN pip install --no-cache-dir -r requirements.txt
14
  # Copy application code
15
  COPY . .
16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  # Expose port
18
  EXPOSE 7860
19
 
20
- # Health check
21
- HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
22
  CMD curl -f http://localhost:7860/health || exit 1
23
 
24
- # Run the application
25
- CMD ["gunicorn", "--bind", "0.0.0.0:7860", "--workers", "1", "--timeout", "120", "app:app"]
 
1
+ # Dockerfile
2
+ FROM python:3.11-slim AS builder
3
+
4
+ # Set environment variables for Python optimization
5
+ ENV PYTHONUNBUFFERED=1 \
6
+ PYTHONDONTWRITEBYTECODE=1 \
7
+ OLLAMA_MODELS=/home/ollama/.ollama \
8
+ OLLAMA_HOST=0.0.0.0
9
+
10
+ # Install build dependencies
11
+ RUN apt-get update && \
12
+ apt-get install -y --no-install-recommends \
13
+ curl \
14
+ wget \
15
+ && apt-get clean && \
16
+ rm -rf /var/lib/apt/lists/*
17
+
18
+ # Install Ollama
19
+ RUN curl -fsSL https://ollama.ai/install.sh | sh
20
+
21
+ # Final stage
22
  FROM python:3.11-slim
23
 
24
+ # Create a non-root user
25
+ RUN useradd -m -u 1000 ollama && \
26
+ mkdir -p /home/ollama/.ollama && \
27
+ chown -R ollama:ollama /home/ollama
28
+
29
  WORKDIR /app
30
 
31
+ # Create logs directory
32
+ RUN mkdir -p /app/logs && \
33
+ chown -R ollama:ollama /app/logs
34
+
35
+ # Copy Ollama binaries from builder stage
36
+ COPY --from=builder /usr/local/bin/ollama /usr/local/bin/ollama
37
 
38
  # Copy requirements and install Python dependencies
39
  COPY requirements.txt .
 
42
  # Copy application code
43
  COPY . .
44
 
45
+ # Set proper ownership and permissions
46
+ RUN chown -R ollama:ollama /app && \
47
+ chmod -R 755 /app
48
+
49
+ # Switch to ollama user
50
+ USER ollama
51
+
52
+ # Create a startup script with configurable model pulling and enhanced logging
53
+ RUN echo '#!/bin/bash\n\
54
+ set -e\n\
55
+ LOG_FILE=/app/logs/startup.log\n\
56
+ mkdir -p /app/logs\n\
57
+ echo "Starting Ollama server at $(date)" >> $LOG_FILE\n\
58
+ ollama serve >> $LOG_FILE 2>&1 &\n\
59
+ sleep 15\n\
60
+ MODELS_TO_PULL="${MODELS_TO_PULL:-hf.co/gguf-org/gemma-3-270m-gguf:Q5_K_S}"\n\
61
+ echo "Pulling models: $MODELS_TO_PULL" | tee -a $LOG_FILE\n\
62
+ IFS=',' read -ra MODEL_ARRAY <<< "$MODELS_TO_PULL"\n\
63
+ for model in "${MODEL_ARRAY[@]}"; do\n\
64
+ echo "Pulling model $model..." | tee -a $LOG_FILE\n\
65
+ for attempt in {1..3}; do\n\
66
+ if ollama pull "$model" >> $LOG_FILE 2>&1; then\n\
67
+ echo "Model $model pulled successfully" | tee -a $LOG_FILE\n\
68
+ break\n\
69
+ else\n\
70
+ echo "Attempt $attempt: Failed to pull model $model, retrying in 10 seconds..." | tee -a $LOG_FILE\n\
71
+ sleep 10\n\
72
+ fi\n\
73
+ if [ $attempt -eq 3 ]; then\n\
74
+ echo "Error: Failed to pull model $model after 3 attempts" | tee -a $LOG_FILE\n\
75
+ exit 1\n\
76
+ fi\n\
77
+ done\n\
78
+ done\n\
79
+ echo "Starting Gunicorn server at $(date)" | tee -a $LOG_FILE\n\
80
+ exec python3 -m gunicorn --bind 0.0.0.0:7860 --workers 1 --timeout 120 --log-level info app:app >> $LOG_FILE 2>&1' > /app/start.sh && \
81
+ chmod +x /app/start.sh
82
+
83
  # Expose port
84
  EXPOSE 7860
85
 
86
+ # Health check with optimized parameters
87
+ HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \
88
  CMD curl -f http://localhost:7860/health || exit 1
89
 
90
+ # Run the startup script
91
+ CMD ["/app/start.sh"]