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