Tuesday, September 23, 2025

Dock erization 230925 backend for Maran

let’s set this up properly with Docker Compose so you can run:

docker compose up -d

and have everything (FastAPI backend + Ollama + NGINX reverse proxy with HTTPS/WebSockets) working together.


πŸ“‚ Project Structure

your-project/
│── main.py
│── service.py
│── requirements.txt
│── Dockerfile
│── nginx.conf
│── docker-compose.yml
│── certs/   (your SSL cert + key go here)

✅ Dockerfile (backend)

You already have this, but let’s finalize:

FROM python:3.11-slim

WORKDIR /app

RUN apt-get update && apt-get install -y \
    build-essential curl \
    && rm -rf /var/lib/apt/lists/*

COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

COPY . .

EXPOSE 8000

CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]

✅ requirements.txt

fastapi
uvicorn[standard]
langchain
langchain-community
langchain-ollama
duckduckgo-search

✅ nginx.conf

events {}

http {
    upstream backend {
        server backend:8000;
    }

    upstream ollama {
        server ollama:11434;
    }

    server {
        listen 80;
        server_name immai.acintia.com;

        # Redirect HTTP → HTTPS
        return 301 https://$host$request_uri;
    }

    server {
        listen 443 ssl;
        server_name immai.acintia.com;

        ssl_certificate /etc/nginx/certs/fullchain.pem;
        ssl_certificate_key /etc/nginx/certs/privkey.pem;

        location / {
            proxy_pass http://backend;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            proxy_set_header Host $host;
        }

        location /ws {
            proxy_pass http://backend;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            proxy_set_header Host $host;
        }

        location /ollama/ {
            proxy_pass http://ollama/;
            proxy_http_version 1.1;
            proxy_set_header Host $host;
        }
    }
}

✅ docker-compose.yml

version: "3.9"

services:
  backend:
    build: .
    container_name: fastapi_backend
    restart: always
    environment:
      - OLLAMA_BASE_URL=http://ollama:11434
    depends_on:
      - ollama

  ollama:
    image: ollama/ollama:latest
    container_name: ollama_service
    restart: always
    ports:
      - "11434:11434"
    volumes:
      - ollama_data:/root/.ollama

  nginx:
    image: nginx:latest
    container_name: nginx_proxy
    restart: always
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf:ro
      - ./certs:/etc/nginx/certs:ro
    depends_on:
      - backend
      - ollama

volumes:
  ollama_data:

✅ Certificates

  • Place your SSL certs inside ./certs/:

    • fullchain.pem

    • privkey.pem

(You can generate free ones with Let’s Encrypt + certbot, or use self-signed for testing).


πŸš€ Run it

docker compose up -d --build
  • FastAPI backend → https://immai.acintia.com

  • WebSockets → wss://immai.acintia.com/ws/query

  • Ollama API proxied → https://immai.acintia.com/ollama/


⚠️ Question for you:
In your main.py, you’re pointing ChatOllama(base_url="https://immai.acintia.com", ...).
πŸ‘‰ Do you want me to adjust that so it talks directly to ollama service inside Docker (http://ollama:11434)? That way it doesn’t loop back through NGINX unnecessarily.


======================================================================

Got it ✅

We’ll make your FastAPI backend talk directly to the ollama service inside Docker, instead of looping through NGINX.

That means in main.py (or wherever you use ChatOllama), change this:

from langchain_community.chat_models import ChatOllama

llm = ChatOllama(
    base_url="https://immai.acintia.com",  # ❌ not needed
    model="llama2:7b",
)

πŸ‘‰ To this:

from langchain_community.chat_models import ChatOllama
import os

ollama_base_url = os.getenv("OLLAMA_BASE_URL", "http://ollama:11434")

llm = ChatOllama(
    base_url=ollama_base_url,
    model="llama2:7b",
)

✅ Why this works

  • Inside docker-compose.yml, we defined:

environment:
  - OLLAMA_BASE_URL=http://ollama:11434
  • So your backend container resolves ollama (Docker DNS) → Ollama container.

  • This avoids unnecessary SSL or reverse proxy overhead when the backend talks to Ollama.

  • External clients (browser, frontend, Postman) will still hit NGINX → FastAPI → Ollama using https://immai.acintia.com.


πŸ”„ Next Steps for You

  1. Update main.py as above.

  2. Rebuild and restart with:

docker compose up -d --build
  1. Test WebSocket:

wscat -c wss://immai.acintia.com/ws/query

Do you want me to also add a /health endpoint in FastAPI so you can quickly confirm backend and Ollama connectivity (like GET /health"backend OK", "ollama OK") before debugging WebSocket?

No comments:

Post a Comment

Dock erization 230925 backend for Maran

let’s set this up properly with Docker Compose so you can run: docker compose up -d and have everything (FastAPI backend + Ollama + NGINX...