Docker Compose For AI Business
To clear up the confusion: for a professional "ChatGPT-like" business, you want two separate containers.
- The API Gateway: Handles your Python logic, Stripe, Redis, and security.
- The Ollama Engine: Dedicated solely to running the AI models on your GPU.
Separating them allows you to restart your code without killing the AI model (which takes a long time to load into VRAM).
1. The Gateway Dockerfile
This is for your FastAPI middleware logic. Save this as Dockerfile in your root folder.
Dockerfile
FROM python:3.10-slim
WORKDIR /app
# Install dependencies
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
# Copy application code
COPY . .
# Start FastAPI
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
2. The Ollama Startup Script (entrypoint.sh)
This script runs inside the Ollama container to ensure the model is pulled automatically. Save this as entrypoint.sh.
Bash
#!/bin/bash
# Start Ollama server in the background
ollama serve &
# Wait for the server to be ready
sleep 5
# Pre-load the model for your customers
echo "Pulling Llama3 model..."
ollama pull llama3
# Keep the process running
wait
3. The Full docker-compose.yml
This connects the Gateway, the Model, and the Database.
YAML
services:
# 1. THE AI ENGINE
ollama:
image: ollama/ollama:latest
container_name: ollama_engine
volumes:
- ./entrypoint.sh:/entrypoint.sh
- ollama_storage:/root/.ollama
entrypoint: ["/bin/bash", "/entrypoint.sh"]
deploy:
resources:
reservations:
devices:
- driver: nvidia
count: 1
capabilities: [gpu]
networks:
- ai_business_network
# 2. THE SECURITY GATEKEEPER (Your Python Code)
gateway:
build: .
container_name: api_gateway
ports:
- "8000:8000"
environment:
- OLLAMA_URL=http://ollama:11434/api/generate
- REDIS_HOST=redis
- STRIPE_SECRET_KEY=${STRIPE_SECRET_KEY}
depends_on:
- ollama
- redis
networks:
- ai_business_network
# 3. THE USAGE TRACKER
redis:
image: redis:alpine
container_name: redis_usage
networks:
- ai_business_network
networks:
ai_business_network:
driver: bridge
volumes:
ollama_storage:
4. How to Launch
- Prepare your files: Ensure main.py, Dockerfile, entrypoint.sh, and requirements.txt are in the same folder.
- Permissions: Run chmod +x entrypoint.sh to make sure Docker can execute the script.
- Environment: Create a .env file in the same folder and add your STRIPE_SECRET_KEY=sk_test_....
- Launch: ```bash
docker-compose up --build -d
Why this is the "Pro" way:
- Persistent AI: The ollama_storage volume means you only download the model once. Even if you delete the containers, the 5GB model stays on your hard drive.
- GPU Isolation: Only the ollama container touches the GPU, keeping your gateway light and fast.
- Security: By using networks, the Ollama API is hidden from the public internet. Only your Python gateway can talk to it.
Would you like me to help you write the requirements.txt file with the exact versions needed to avoid "dependency hell"?