# Docker Configuration for MatematikaNusantara

This directory contains production-ready Docker configuration for the MatematikaNusantara Laravel + React (Inertia.js) application.

## Quick Start

### Windows (PowerShell)
```powershell
.\docker-setup.ps1
```

### Linux/macOS
```bash
chmod +x docker-setup.sh
./docker-setup.sh
```

## Manual Setup

### 1. Create Environment File
```bash
cp .env.docker .env
```

Edit `.env` and configure:
- `APP_KEY` (will be auto-generated)
- Database credentials
- Mail settings
- Payment gateway keys (Midtrans)
- Google OAuth credentials

### 2. Build and Start
```bash
docker-compose -f docker-compose.prod.yml build
docker-compose -f docker-compose.prod.yml up -d
```

### 3. Initialize Application
```bash
# Generate app key
docker-compose -f docker-compose.prod.yml exec app php artisan key:generate

# Run migrations
docker-compose -f docker-compose.prod.yml exec app php artisan migrate --force

# Create storage link
docker-compose -f docker-compose.prod.yml exec app php artisan storage:link

# Cache configuration
docker-compose -f docker-compose.prod.yml exec app php artisan config:cache
docker-compose -f docker-compose.prod.yml exec app php artisan route:cache
docker-compose -f docker-compose.prod.yml exec app php artisan view:cache
```

## Architecture

### Services

**app** - Main Laravel application
- PHP 8.2-FPM + Nginx + Supervisor
- Handles web requests and background queue workers
- Port: 8080 (default)

**mysql** - MySQL 8.0 database
- Persistent data storage
- Port: 3306

**redis** - Redis cache server
- Session and cache storage
- Port: 6379

### Multi-Stage Build

The Dockerfile uses multi-stage builds for optimal image size:

1. **node-builder**: Compiles React assets with Vite
2. **composer-builder**: Installs PHP dependencies
3. **Final stage**: Combines compiled assets with PHP runtime

### Best Practices Implemented

✅ Multi-stage builds for minimal image size  
✅ Layer caching optimization  
✅ Non-root user for security  
✅ Production PHP configuration (OPcache enabled)  
✅ Supervisor for process management  
✅ Health checks for all services  
✅ Volume mounts for persistent data  
✅ .dockerignore to exclude unnecessary files  
✅ Alpine-based images for smaller footprint

## Common Commands

### View Logs
```bash
# All services
docker-compose -f docker-compose.prod.yml logs -f

# Specific service
docker-compose -f docker-compose.prod.yml logs -f app
```

### Execute Artisan Commands
```bash
docker-compose -f docker-compose.prod.yml exec app php artisan [command]
```

### Access Database
```bash
docker-compose -f docker-compose.prod.yml exec mysql mysql -u laravel -p
```

### Access Redis CLI
```bash
docker-compose -f docker-compose.prod.yml exec redis redis-cli
```

### Restart Services
```bash
docker-compose -f docker-compose.prod.yml restart
```

### Stop Services
```bash
docker-compose -f docker-compose.prod.yml down
```

### Rebuild Image
```bash
docker-compose -f docker-compose.prod.yml build --no-cache
```

## Troubleshooting

### Permission Issues
```bash
# Fix storage permissions
docker-compose -f docker-compose.prod.yml exec app chown -R appuser:appuser storage bootstrap/cache
```

### Clear Cache
```bash
docker-compose -f docker-compose.prod.yml exec app php artisan cache:clear
docker-compose -f docker-compose.prod.yml exec app php artisan config:clear
docker-compose -f docker-compose.prod.yml exec app php artisan route:clear
docker-compose -f docker-compose.prod.yml exec app php artisan view:clear
```

### Database Connection Issues
```bash
# Check MySQL status
docker-compose -f docker-compose.prod.yml exec mysql mysqladmin ping -h localhost -u root -p

# Verify environment variables
docker-compose -f docker-compose.prod.yml exec app env | grep DB_
```

### View Running Processes
```bash
docker-compose -f docker-compose.prod.yml exec app supervisorctl status
```

## Production Deployment

### Environment Variables
Ensure these are set in `.env`:
- `APP_ENV=production`
- `APP_DEBUG=false`
- `FORCE_HTTPS=true` (if using HTTPS)
- Strong `DB_PASSWORD`
- Production API keys (Midtrans, Google OAuth)

### Security Considerations
- Use strong passwords for MySQL root and user
- Enable Redis password protection
- Set up SSL/TLS certificates (use reverse proxy like Nginx or Traefik)
- Regular security updates: `docker-compose pull`
- Backup database regularly

## Volume Management

### Backup Database
```bash
docker-compose -f docker-compose.prod.yml exec mysql mysqldump -u laravel -p laravel > backup.sql
```

### Restore Database
```bash
cat backup.sql | docker-compose -f docker-compose.prod.yml exec -T mysql mysql -u laravel -p laravel
```

### Clear Volumes
```bash
docker-compose -f docker-compose.prod.yml down -v
```

## Performance Optimization

The configuration includes:
- OPcache enabled with optimized settings
- Redis for sessions and cache
- Nginx serving static files directly
- Queue workers running in background
- Compiled and cached views/routes/config

## Support

For issues or questions, refer to:
- Laravel documentation: https://laravel.com/docs
- Docker documentation: https://docs.docker.com
