Notes_App / Dockerfile
pavan234Reddy's picture
Update Dockerfile
00056d8 verified
Raw History Blame Contribute Delete
1.72 kB
FROM python:3.11-slim
# Install system dependencies
RUN apt-get update && apt-get install -y \
curl \
&& rm -rf /var/lib/apt/lists/*
# Create a non-root user
RUN useradd -m -u 1000 user
USER user
WORKDIR /app
# Copy application files
COPY --chown=user:user requirements.txt .
COPY --chown=user:user main.py .
# Install Python dependencies
RUN pip install --user --no-cache-dir -r requirements.txt
# Create app directory and set permissions
RUN mkdir -p /home/user/app && chown user:user /home/user/app
# Install Ollama for user
RUN curl -fsSL https://ollama.ai/install.sh | sh
# Set environment variables
ENV PATH="/home/user/.local/bin:${PATH}"
ENV OLLAMA_HOST="0.0.0.0"
ENV HOME="/home/user"
# Create startup script
RUN echo '#!/bin/bash\n\
\n\
echo "=== Starting Ollama FastAPI Server on Hugging Face === "\n\
\n\
# Set Ollama data directory to user home\n\
export OLLAMA_MODELS="/home/user/.ollama/models"\n\
\n\
# Start Ollama in background\n\
echo "Starting Ollama service..."\n\
ollama serve &\n\
\n\
# Wait for Ollama to start\n\
echo "Waiting for Ollama to start..."\n\
sleep 15\n\
\n\
# Check if Ollama is running\n\
if curl -s http://localhost:11434/api/tags > /dev/null; then\n\
echo "Ollama is running!"\n\
\n\
# Pull a small model\n\
echo "Pulling tinyllama:1.1b model..."\n\
ollama pull tinyllama:1.1b &\n\
\n\
else\n\
echo "Ollama failed to start. Starting FastAPI without Ollama..."\n\
fi\n\
\n\
echo "Starting FastAPI server on port 7860..."\n\
# Start FastAPI server\n\
python -m uvicorn main:app --host 0.0.0.0 --port 7860\n\
' > start.sh && chmod +x start.sh
# Expose port (HF Spaces uses 7860)
EXPOSE 7860
# Start the application
CMD ["./start.sh"]