# Running Multiple Nanobot Gateways with Docker

This guide shows you how to run multiple nanobot gateway instances, each with its own Telegram bot.

## Quick Start

### Step 1: Setup Config Directories

Run the setup script:

```bash
./multi-bot-setup.sh
```

Or manually:

```bash
# Create directories
mkdir -p ~/.nanobot-user1
mkdir -p ~/.nanobot-user2
mkdir -p ~/.nanobot-user3

# Copy your base config (if you have one)
cp ~/.nanobot/config.json ~/.nanobot-user1/config.json
cp ~/.nanobot/config.json ~/.nanobot-user2/config.json
cp ~/.nanobot/config.json ~/.nanobot-user3/config.json
```

### Step 2: Configure Each Bot

Edit each config file with different Telegram bot tokens:

**`~/.nanobot-user1/config.json`:**
```json
{
  "providers": {
    "openrouter": {
      "apiKey": "sk-or-v1-xxx"
    }
  },
  "agents": {
    "defaults": {
      "model": "anthropic/claude-opus-4-5"
    }
  },
  "channels": {
    "telegram": {
      "enabled": true,
      "token": "1234567890:ABCdefGHIjklMNOpqrsTUVwxyz",
      "allowFrom": ["123456789"]
    }
  }
}
```

**`~/.nanobot-user2/config.json`:**
```json
{
  "providers": {
    "openrouter": {
      "apiKey": "sk-or-v1-xxx"
    }
  },
  "agents": {
    "defaults": {
      "model": "anthropic/claude-opus-4-5"
    }
  },
  "channels": {
    "telegram": {
      "enabled": true,
      "token": "9876543210:XYZabcDEFghiJKLmnopQRSuvwx",
      "allowFrom": ["987654321"]
    }
  }
}
```

**`~/.nanobot-user3/config.json`:**
```json
{
  "providers": {
    "openrouter": {
      "apiKey": "sk-or-v1-xxx"
    }
  },
  "agents": {
    "defaults": {
      "model": "anthropic/claude-opus-4-5"
    }
  },
  "channels": {
    "telegram": {
      "enabled": true,
      "token": "5551234567:LMNopqRSTuvwXYZabcdefGHIjkl",
      "allowFrom": ["555123456"]
    }
  }
}
```

### Step 3: Run with Docker Compose (Recommended)

```bash
# Build the image (first time only)
docker compose -f docker-compose.multi.yml build

# Start all bots
docker compose -f docker-compose.multi.yml up -d

# View logs
docker compose -f docker-compose.multi.yml logs -f

# Stop all bots
docker compose -f docker-compose.multi.yml down

# Start/stop individual bots
docker compose -f docker-compose.multi.yml start nanobot-user1
docker compose -f docker-compose.multi.yml stop nanobot-user2
```

### Step 4: Run with Docker Run Commands (Alternative)

If you prefer `docker run` commands:

```bash
# Build the image first
docker build -t nanobot .

# Run bot 1
docker run -d \
  --name nanobot-user1 \
  -v ~/.nanobot-user1:/root/.nanobot \
  -p 18790:18790 \
  --restart unless-stopped \
  nanobot gateway

# Run bot 2
docker run -d \
  --name nanobot-user2 \
  -v ~/.nanobot-user2:/root/.nanobot \
  -p 18791:18790 \
  --restart unless-stopped \
  nanobot gateway

# Run bot 3
docker run -d \
  --name nanobot-user3 \
  -v ~/.nanobot-user3:/root/.nanobot \
  -p 18792:18790 \
  --restart unless-stopped \
  nanobot gateway
```

## Managing Containers

### View Logs

```bash
# All bots
docker compose -f docker-compose.multi.yml logs -f

# Specific bot
docker logs -f nanobot-user1

# Or with docker-compose
docker compose -f docker-compose.multi.yml logs -f nanobot-user1
```

### Stop/Start Containers

```bash
# Stop all
docker compose -f docker-compose.multi.yml down

# Start all
docker compose -f docker-compose.multi.yml up -d

# Restart specific bot
docker restart nanobot-user1

# Stop specific bot
docker stop nanobot-user1
docker start nanobot-user1
```

### Check Status

```bash
# List all running containers
docker ps | grep nanobot

# Check logs for errors
docker logs nanobot-user1 --tail 50
```

## Port Mapping

Each bot uses a different host port:
- **User 1**: Port `18790` → Container port `18790`
- **User 2**: Port `18791` → Container port `18790`
- **User 3**: Port `18792` → Container port `18790`

The gateway port inside the container is always `18790`, but mapped to different host ports to avoid conflicts.

## Creating Telegram Bots

For each user, create a separate bot:

1. Open Telegram, search `@BotFather`
2. Send `/newbot`
3. Follow prompts to create a bot
4. Copy the token
5. Add it to the respective config file

## Troubleshooting

### Bot not responding

```bash
# Check if container is running
docker ps | grep nanobot-user1

# Check logs for errors
docker logs nanobot-user1

# Verify config is correct
cat ~/.nanobot-user1/config.json | jq '.channels.telegram'
```

### Port already in use

If you get port conflicts, change the port mappings in `docker-compose.multi.yml`:

```yaml
ports:
  - "18890:18790"  # Change 18790 to 18890
```

### Config not loading

Make sure the volume mount is correct:
```bash
# Verify config exists
ls -la ~/.nanobot-user1/config.json

# Check if it's readable
docker exec nanobot-user1 cat /root/.nanobot/config.json
```

## Adding More Bots

To add more bots:

1. Create new directory: `mkdir -p ~/.nanobot-user4`
2. Copy config: `cp ~/.nanobot-user1/config.json ~/.nanobot-user4/config.json`
3. Edit config with new bot token
4. Add new service to `docker-compose.multi.yml` (copy existing service, change name and port)
5. Run: `docker compose -f docker-compose.multi.yml up -d nanobot-user4`


