# ChurchCRM Docker - Startup & Commands Guide

## 📋 Quick Start

### Initial Setup (First Time)
```bash
# Navigate to project directory
cd /home/backend_dev/Javascript/ChurchCRM-7.3.3/churchcrm

# Build and start containers
docker-compose up -d --build

# Wait 10-15 seconds for services to initialize
sleep 15



# Access setup wizard
# Browser: http://localhost:8000/setup/
```

### Regular Startup (After Initial Setup)
```bash
# Navigate to project directory
cd /home/backend_dev/Javascript/ChurchCRM-7.3.3/churchcrm

# Start containers
docker-compose up -d

# Wait for services to be ready
sleep 5

# Access the application
# Browser: http://localhost:8000/
```

---

## 🔌 Docker Compose Commands

### Start Containers
```bash
# Start in background (recommended)
docker-compose up -d

# Start and view logs
docker-compose up
```

### Stop Containers
```bash
# Stop containers (data persists)
docker-compose stop

# Stop and remove containers (data persists in volumes)
docker-compose down

# Stop, remove containers and volumes (removes database!)
docker-compose down -v
```

### View Status & Logs
```bash
# Check container status
docker-compose ps

# View PHP container logs
docker-compose logs php

# View database container logs
docker-compose logs db

# View last 50 lines of PHP logs
docker-compose logs php --tail=50

# Follow logs in real-time
docker-compose logs -f php
```

### Rebuild (After Dockerfile changes)
```bash
# Rebuild and restart
docker-compose down && docker-compose up -d --build

# Rebuild without restarting
docker-compose build --no-cache
```

### Execute Commands in Containers
```bash
# Access PHP container shell
docker exec -it churchcrm-php bash

# Access database container shell
docker exec -it churchcrm-db bash

# Run PHP command
docker exec churchcrm-php php -v

# Run MySQL command
docker exec churchcrm-db mysql -u churchcrm -pchurchcrm churchcrm
```

### Restart Containers
```bash
# Restart all containers
docker-compose restart

# Restart specific container
docker-compose restart php
docker-compose restart db
```

---

## 🌐 Application Routes & URLs

### Setup & Installation
- **Initial Setup Wizard:** http://localhost:8000/setup/
- **System Check:** http://localhost:8000/setup/SystemPrerequisiteCheck
- **Filesystem Check:** http://localhost:8000/setup/SystemFilesystemCheck
- **Integrity Check:** http://localhost:8000/setup/SystemIntegrityCheck
- **Locale Check:** http://localhost:8000/setup/SystemLocaleCheck

### Main Application
- **Home/Dashboard:** http://localhost:8000/
- **Login:** http://localhost:8000/ (redirects if not logged in)
- **Admin Panel:** http://localhost:8000/admin/
- **People/Members:** http://localhost:8000/people/
- **Groups:** http://localhost:8000/groups/
- **Finance:** http://localhost:8000/finance/
- **Events:** http://localhost:8000/event/
- **Reports:** http://localhost:8000/Reports/

### API Endpoints
- **API Base:** http://localhost:8000/api/
- **OpenAPI Docs:** http://localhost:8000/api/openapi/

---

## 🗄️ Database Credentials

### Connection Details
- **Host:** `db` (inside Docker) / `localhost` (from host machine)
- **Port:** `3306`
- **Database Name:** `churchcrm`
- **Username:** `churchcrm`
- **Password:** `churchcrm`
- **Root Password:** `root`

### Connect from Host Machine
```bash
# Using mysql client (if installed)
mysql -h localhost -u churchcrm -pchurchcrm churchcrm

# Or use phpMyAdmin style tools
# Host: localhost:3306
# User: churchcrm
# Password: churchcrm
```

### Access Database from Container
```bash
docker exec -it churchcrm-db mysql -u churchcrm -pchurchcrm churchcrm
```

---

## 👤 Application Login

**Default Admin Credentials:**
- **Username:** `admin`
- **Password:** `changeme`

⚠️ **Change password immediately after first login!**

---

## 📁 Important Directories in Container

- **Application Root:** `/var/www/html/`
- **Configuration:** `/var/www/html/Include/Config.php`
- **Database Backups:** `/var/www/html/mysql/`
- **Images:** `/var/www/html/Images/`
- **Logs:** `/var/www/html/logs/`
- **Session Data:** `/var/www/html/session/`
- **Attachments:** `/var/www/html/tmp_attach/`

---

## 🛠️ Common Troubleshooting Commands

### Check Service Health
```bash
# List all containers
docker ps

# View detailed container info
docker inspect churchcrm-php
docker inspect churchcrm-db

# Test database connectivity
docker exec churchcrm-php mysqli_connect('db', 'churchcrm', 'churchcrm')
```

### Fix Permissions
```bash
# Reset permissions on writable directories
docker exec churchcrm-php bash -c 'chmod -R 777 Include Images logs session tmp_attach'

# Restart to apply entrypoint script fix
docker-compose restart php
```

### Clear Cache/Temp Files
```bash
# Clean tmp_attach directory
docker exec churchcrm-php rm -rf /var/www/html/tmp_attach/*

# Clear logs
docker exec churchcrm-php truncate -s 0 /var/www/html/logs/*.log
```

### Reset Application
```bash
# Stop everything and remove volumes (WARNING: deletes database!)
docker-compose down -v

# Rebuild and start fresh
docker-compose up -d --build

# Access setup wizard
# http://localhost:8000/setup/
```

---

## 📊 Ports Used

| Service | Container Port | Host Port | Protocol |
|---------|---|---|---|
| Apache/PHP | 80 | 8000 | HTTP |
| MariaDB | 3306 | 3306 | TCP |

---

## 💾 Backup Commands

### Backup Database
```bash
# Export entire database
docker exec churchcrm-db mysqldump -u churchcrm -pchurchcrm churchcrm > backup.sql

# Export to file in container
docker exec churchcrm-db mysqldump -u churchcrm -pchurchcrm churchcrm > /var/www/html/mysql/backup_$(date +%Y%m%d).sql
```

### Backup Files
```bash
# Backup entire application directory
docker cp churchcrm-php:/var/www/html ./churchcrm_backup_$(date +%Y%m%d)
```

### Restore Database
```bash
# Restore from SQL file
docker exec -i churchcrm-db mysql -u churchcrm -pchurchcrm churchcrm < backup.sql
```

---

## 🚀 Full Example Workflow

```bash
# 1. Navigate to project
cd /home/backend_dev/Javascript/ChurchCRM-7.3.3/churchcrm

# 2. Start services
docker-compose up -d --build

# 3. Wait for startup
sleep 15

# 4. Verify containers are running
docker-compose ps

# 5. Open browser and navigate to setup
# http://localhost:8000/setup/

# 6. Configure with these credentials:
#    Host: db
#    Port: 3306
#    Database: churchcrm
#    User: churchcrm
#    Password: churchcrm

# 7. After setup, access application
# http://localhost:8000/

# 8. Login with
#    Username: admin
#    Password: changeme

# 9. To check logs anytime
docker-compose logs -f php

# 10. To stop when done
docker-compose stop
```

---

## 📞 Quick Reference

| Task | Command |
|------|---------|
| **Start** | `docker-compose up -d` |
| **Stop** | `docker-compose stop` |
| **Status** | `docker-compose ps` |
| **Logs** | `docker-compose logs php` |
| **Restart** | `docker-compose restart` |
| **Rebuild** | `docker-compose build --no-cache` |
| **Shell** | `docker exec -it churchcrm-php bash` |
| **MySQL** | `docker exec -it churchcrm-db mysql -u churchcrm -pchurchcrm churchcrm` |
| **Full Reset** | `docker-compose down -v && docker-compose up -d --build` |

---

## ✅ Verification Checklist

- [ ] Containers running: `docker-compose ps`
- [ ] PHP container accessible: `docker exec churchcrm-php echo "OK"`
- [ ] Database accessible: `docker exec churchcrm-db mysql -u churchcrm -pchurchcrm churchcrm -e "SELECT 1"`
- [ ] Web server responding: `curl http://localhost:8000/`
- [ ] Setup wizard visible: `curl http://localhost:8000/setup/ | grep -i setup`
- [ ] Config file exists: `docker exec churchcrm-php test -f /var/www/html/Include/Config.php && echo "OK"`
