# Use Ubuntu as base image for better compatibility FROM ubuntu:22.04 # Set environment variables ENV DEBIAN_FRONTEND=noninteractive ENV PYTHONUNBUFFERED=1 ENV PYTHONPATH=/app ENV OLLAMA_HOST=0.0.0.0 ENV TRANSFORMERS_CACHE=/app/.cache/huggingface ENV HF_HOME=/app/.cache/huggingface # Install system dependencies RUN apt-get update && apt-get install -y \ curl \ wget \ git \ python3.10 \ python3-pip \ python3.10-venv \ && rm -rf /var/lib/apt/lists/* # Create symbolic link for python3 RUN ln -sf /usr/bin/python3.10 /usr/bin/python3 # Create a non-root user RUN useradd -m -s /bin/bash appuser # Create necessary directories with proper permissions RUN mkdir -p /app/.cache/huggingface \ && mkdir -p /app/.ollama \ && chown -R appuser:appuser /app \ && chown -R appuser:appuser /app/.cache \ && chown -R appuser:appuser /app/.ollama # Install Ollama RUN curl -fsSL https://ollama.com/install.sh | sh # Set working directory WORKDIR /app # Copy requirements first to leverage Docker cache COPY requirements.txt . # Install Python dependencies RUN pip3 install --no-cache-dir -r requirements.txt # Copy the application code COPY . . # Set proper ownership of all files RUN chown -R appuser:appuser /app # Create a startup script with better logging and error handling RUN echo '#!/bin/bash\n\ set -e\n\ \n\ echo "Starting Healthcare AI Assistant..."\n\ \n\ # Function to check if Ollama is running\n\ check_ollama() {\n\ curl -s http://localhost:11434/api/tags > /dev/null\n\ return $?\n\ }\n\ \n\ # Start Ollama in the background\n\ echo "Starting Ollama server..."\n\ ollama serve &\n\ OLLAMA_PID=$!\n\ \n\ # Wait for Ollama to start\n\ echo "Waiting for Ollama to start..."\n\ for i in {1..30}; do\n\ if check_ollama; then\n\ echo "Ollama server is running"\n\ break\n\ fi\n\ if [ $i -eq 30 ]; then\n\ echo "Error: Ollama server failed to start"\n\ exit 1\n\ fi\n\ sleep 1\n\ done\n\ \n\ # Pull Mistral model\n\ echo "Pulling Mistral model..."\n\ ollama pull mistral\n\ \n\ # Start FastAPI application\n\ echo "Starting FastAPI application..."\n\ cd /app\n\ \n\ # Keep the container running and show logs\n\ exec uvicorn main:app --host 0.0.0.0 --port 8000 --log-level info\n\ ' > /app/start.sh && chmod +x /app/start.sh # Expose ports EXPOSE 8000 11434 # Switch to non-root user USER appuser # Set the entrypoint ENTRYPOINT ["/app/start.sh"]