==============================================================================
DOCKER SETUP INSTRUCTIONS - REPUTATION REBRANDING TOOLKIT
==============================================================================
Author: Pixelated Dwarf
Version: 3.0
Date: October 2026
License: Apache-2.0

PURPOSE:
This file explains how to configure Docker and Ollama for your system.
Choose the section that matches your setup. The docker-compose.yml file
has been simplified; reference this document for detailed options.

==============================================================================
TABLE OF CONTENTS
==============================================================================
1. STANDARD SETUP (Fresh Install with Ollama in Docker)
2. EXISTING OLLAMA ON HOST MACHINE (Recommended for Desktops)
3. REMOTE CLOUD OLLAMA INSTANCE (For Low-Spec Hardware)
4. OLLAMA NETWORK CONFIGURATION (Critical Step)
5. VOLUME MOUNTS EXPLAINED
6. TROUBLESHOOTING COMMON ISSUES
7. START/STOP COMMANDS

==============================================================================
1. STANDARD SETUP (Fresh Install with Ollama in Docker)
==============================================================================

Use this if: You do NOT have Ollama installed on your host machine yet.

Step 1: No changes needed to docker-compose.yml
    The default file includes both 'app' and 'ollama' services.

Step 2: Start the system
    docker-compose up --build -d

Step 3: Pull your models ONCE inside the container
    docker-compose exec app ollama pull llama3:8b
    docker-compose exec app ollama pull phi3:mini

Pros:
    - Everything contained in Docker
    - No host dependencies
Cons:
    - Ollama models live inside the Docker volume
    - Slightly slower GPU access depending on host configuration

==============================================================================
2. EXISTING OLLAMA ON HOST MACHINE (RECOMMENDED FOR MOST USERS)
==============================================================================

Use this if: You already run Ollama locally and want to keep your models there.

Benefits:
    - Faster (direct hardware access, no VM overhead)
    - Models persist without Docker volumes
    - Single point of model management

Configuration Steps:

A. Comment out the 'ollama' service in docker-compose.yml
   
   Find this section and add hash marks (#) to each line:
   
   # ollama:
   #   image: ollama/ollama:latest
   #   container_name: rep-rebrand-ollama
   #   ports:
   #     - "11434:11434"
   #   volumes:
   #     - ollama_data:/root/.ollama
   #   restart: unless-stopped
   #   networks:
   #     - rep-rebrand-net

B. Update the environment variable in the 'app' service:
   
   Find this line:
   - OLLAMA_HOST=http://ollama:11434
   
   Change to:
   - OLLAMA_HOST=host.docker.internal:11434
   
   Note: On Linux, you may need to use --add-host=host.docker.internal:host-gateway 
   in the docker-compose.yml or pass it via extra_hosts. Add this under the 'app' service:
   
   extra_hosts:
     - "host.docker.internal:host-gateway"

C. Ensure Ollama listens on all interfaces on the host:
   
   Set environment variable before starting Ollama:
   export OLLAMA_HOST=0.0.0.0:11434
   ollama serve
   
   Or edit /etc/systemd/system/ollama.service to include:
   Environment="OLLAMA_HOST=0.0.0.0:11434"
   
   Then reload systemd: sudo systemctl daemon-reload && sudo systemctl restart ollama

D. Verify connectivity from the container:
   Inside the app container, try:
   curl http://host.docker.internal:11434/api/tags

==============================================================================
3. REMOTE CLOUD OLLAMA INSTANCE (FOR LOW-SPEC HARDWARE)
==============================================================================

Use this if: Your local machine cannot run the large models (e.g., 8GB RAM).

Benefits:
    - Offloads heavy computation to powerful remote server
    - Local machine only handles orchestration
    - Cost-effective for intermittent usage

Configuration Steps:

A. Set up Ollama on a cloud VPS:
   - Rent a VPS with sufficient RAM/CPU (at least 16GB recommended)
   - Install Ollama following their documentation at ollama.com
   - Configure firewall to allow port 11434
   
B. Update docker-compose.yml app service:
   Set the environment variable:
   - OLLAMA_HOST=https://your-vps-ip:11434
   
C. Secure your connection (recommended):
   Use SSH tunneling instead of exposing Ollama publicly:
   ssh -L 11434:localhost:11434 user@your-vps.com &
   
   Then set:
   - OLLAMA_HOST=http://localhost:11434

D. Pull models ONCE on the remote server:
   ssh user@your-vps.com
   ollama pull llama3:70b
   ollama pull llama3:8b
   ollama pull phi3:mini

==============================================================================
4. OLLAMA NETWORK CONFIGURATION (CRITICAL STEP)
==============================================================================

The containers communicate over an internal Docker network. This section
explains how to ensure proper connectivity.

Default Network (Recommended for standard setups):
- Service name 'ollama' resolves within the docker-compose network
- App container uses OLLAMA_HOST=http://ollama:11434
- No external exposure needed

Host Network Mode (Advanced):
If you experience connectivity issues with Ollama on host:
- Add to 'app' service: network_mode: "host"
- WARNING: May conflict with other applications using port 11434

Linux-Specific Fix for Host.docker.internal:
On Linux systems, 'host.docker.internal' does not work by default.
Add this to docker-compose.yml under the 'app' service:

extra_hosts:
  - "host.docker.internal:host-gateway"

Firewall Considerations:
If using host Ollama, ensure port 11434 is accessible:
sudo ufw allow 11434/tcp

==============================================================================
5. VOLUME MOUNTS EXPLAINED
==============================================================================

The docker-compose.yml mounts several directories for data persistence:

./config:/app/config
    Contains: identity_matrix.yml, model_settings.yml, baseline.db
    Purpose: All your personal identity and threat data
    Note: These files should NEVER be committed to Git (.gitignore handles this)

./generate:/app/generate
    Contains: generator.py, templates/, output/
    Purpose: Content generation scripts and staging area

./content:/app/content
    Contains: validated/, archived/
    Purpose: Final HTML pages ready for deployment

./deploy:/app/deploy
    Contains: manual_helper.py, index_ping.py, ping_broadcast.py
    Purpose: Deployment helpers and task lists

./logs:/app/logs
    Contains: System logs, runtime errors
    Purpose: Debugging failed runs

./backups:/app/backups
    Contains: Manual snapshots of config and content
    Purpose: Disaster recovery

./recon:/app/recon
    Contains: scan.py
    Purpose: Threat scanning scripts

==============================================================================
6. TROUBLESHOOTING COMMON ISSUES
==============================================================================

Issue: Cannot connect to Docker daemon
Solution: 
  - Check if Docker is running: systemctl status docker
  - Add your user to docker group: sudo usermod -aG docker $USER
  - Log out and back in for group change to take effect

Issue: Permission denied on volume mounts
Solution:
  - Run as non-root: docker-compose up --build -d
  - If still failing, adjust permissions: chmod -R 755 ./config ./content

Issue: Ollama API connection refused
Solution:
  - Check Ollama is running: ps aux | grep ollama
  - Test endpoint directly: curl http://localhost:11434/api/tags
  - Verify OLLAMA_HOST env var matches your setup

Issue: Playwright browser launch failed
Solution:
  - Reinstall browsers inside container: playwright install chromium firefox webkit
  - Reinstall system deps: playwright install-deps chromium firefox webkit
  - Check you have enough disk space for browser binaries (2GB+)

Issue: Model not found error
Solution:
  - Pull the required models: docker-compose exec app ollama pull llama3:70b
  - Verify available models: docker-compose exec app ollama list
  - Model names are case-sensitive: llama3:70b, not LLAMA3:70B

Issue: Database locked error
Solution:
  - Close any other processes accessing baseline.db
  - Stop containers: docker-compose down
  - Delete and rebuild: rm config/baseline.db && docker-compose up --build

Issue: Container exits immediately after start
Solution:
  - Check logs: docker-compose logs app
  - The CMD should keep container running for interactive use
  - If debugging, add tail -f /dev/null as last command

==============================================================================
7. START/STOP COMMANDS
==============================================================================

First Time Setup:
1. Build and start everything:
   docker-compose up --build -d

2. Enter the application shell:
   docker-compose exec app bash

3. Run the wizard to generate config:
   python3 setup/setup_wizard.py

4. Start reconnaissance scan:
   python3 recon/scan.py

Daily Operations:
- Start all services: docker-compose up -d
- Stop all services: docker-compose down
- View logs: docker-compose logs -f app
- Enter app shell: docker-compose exec app bash
- Restart app only: docker-compose restart app
- Restart ollama only: docker-compose restart ollama

Maintenance:
- Prune unused images: docker image prune -f
- Prune stopped containers: docker container prune -f
- Backup config before updates: cp -r config backups/config-backup-$(date +%Y%m%d)

Cleanup (Full Reset):
WARNING: This deletes ALL data including configs and scan results!
docker-compose down -v
rm -rf config/* content/* logs/* backups/*

After cleanup, re-run docker-compose up --build from scratch.

==============================================================================
END OF DOCUMENT
==============================================================================