# Docker Compose - Gelişmiş Özellikler

## 🚀 Yeni Eklenen Özellikler

### 1. **Persistent Volumes (Kalıcı Depolama)**
```yaml
volumes:
  - freecad_logs:/var/log        # FreeCAD API logları
  - go_build_cache:/go/pkg/mod   # Go build cache (hızlı rebuild)
```

**Faydası**:
- Container yeniden başlatıldığında loglar kaybolmaz
- Go modülleri cache'lenir, rebuild 10x daha hızlı

**Yönetim**:
```bash
# Volume'ları listele
docker volume ls | grep fikoyt

# Log'ları görüntüle
docker run --rm -v fikoyt_freecad_logs:/logs alpine cat /logs/freecad_api.log

# Volume'ları temizle
docker volume rm fikoyt_freecad_logs fikoyt_go_build_cache
```

### 2. **Health Check (Sağlık Kontrolü)**
```yaml
healthcheck:
  test: ["CMD", "sh", "-c", "curl -f http://localhost:9000/health 2>/dev/null || exit 1"]
  interval: 30s      # Her 30 saniyede kontrol
  timeout: 10s       # 10 saniye timeout
  retries: 3         # 3 başarısız deneme
  start_period: 60s  # İlk 60 saniye bekleme süresi
```

**Faydası**:
- Container çökmüş mü otomatik tespit
- `docker ps` ile status görünür (healthy/unhealthy)
- Orchestration araçları (Kubernetes, Swarm) ile uyumlu

**Kontrol**:
```bash
docker ps  # STATUS sütununda "healthy" görmeli
docker inspect fikoyt-sunucu | grep Health -A 20
```

### 3. **Resource Limits (Kaynak Sınırları)**
```yaml
deploy:
  resources:
    limits:
      cpus: '4.0'      # Max 4 CPU core
      memory: 8G       # Max 8GB RAM
```

**Faydası**:
- Container sistem kaynaklarını aşırı kullanamaz
- Diğer servislere etki etmez
- OOM (Out of Memory) hatalarını önler

**Monitoring**:
```bash
docker stats fikoyt-sunucu
```

### 4. **Logging Configuration (Log Yönetimi)**
```yaml
logging:
  driver: "json-file"
  options:
    max-size: "10m"   # Max dosya boyutu
    max-file: "3"     # Max dosya sayısı
```

**Faydası**:
- Disk dolmasını önler
- Max 30MB log (10MB x 3 dosya)
- Otomatik log rotation

**Log Görüntüleme**:
```bash
# Son 100 satır
docker logs fikoyt-sunucu --tail 100

# Canlı izleme
docker logs fikoyt-sunucu -f

# Tarih filtreleme
docker logs fikoyt-sunucu --since 1h
```

### 5. **Environment Variables (Ortam Değişkenleri)**
Yeni eklenenler:
```yaml
- PYTHONUNBUFFERED=1      # Python print() hemen görünür
- AIR_ENABLED=true        # Go hot reload
- MONGO_HOST=mongodb      # MongoDB host (network üzerinden)
- TZ=Europe/Istanbul      # Timezone
```

### 6. **Restart Policy (Yeniden Başlatma)**
```yaml
restart: unless-stopped
```

**Davranış**:
- ✅ Sistem reboot sonrası otomatik başlar
- ✅ Crash durumunda yeniden başlar
- ❌ Manuel `docker stop` sonrası başlamaz

**Alternatifler**:
- `always`: Her zaman başlat (manuel stop bile)
- `on-failure`: Sadece hata durumunda
- `no`: Hiç başlatma

### 7. **Network Configuration**
```yaml
networks:
  fikoyt_ag:
    external: true
    name: fikoyt_ag
```

**Faydası**:
- MongoDB container'ı ile iletişim
- DNS resolution: `mongodb:27017`
- İzole network güvenliği

## 📊 Kullanım Senaryoları

### Senaryo 1: Container Crash Durumu
```bash
# Container crash oldu
docker ps -a  # STATUS: Exited (1)

# Health check failed mı?
docker inspect fikoyt-sunucu | grep Health

# Log'lara bak
docker logs fikoyt-sunucu --tail 50

# Restart policy otomatik yeniden başlatır
# Manuel yeniden başlatmak için:
docker restart fikoyt-sunucu
```

### Senaryo 2: Disk Dolu
```bash
# Log boyutları kontrol et
docker system df

# Eski logları temizle
docker system prune --volumes

# Log rotation otomatik çalışır (max 30MB)
```

### Senaryo 3: Performans Sorunları
```bash
# Resource kullanımı izle
docker stats fikoyt-sunucu

# CPU/Memory limitleri aşıyor mu?
# docker-compose.yml'de limitleri artır:
# cpus: '8.0'
# memory: 16G

# Rebuild
docker-compose up -d
```

### Senaryo 4: MongoDB Bağlantı Sorunu
```bash
# Network kontrolü
docker network ls | grep fikoyt_ag
docker network inspect fikoyt_ag

# MongoDB container'ı aynı network'te mi?
docker network inspect fikoyt_ag | grep mongodb

# Container içinden test
docker exec fikoyt-sunucu ping mongodb
docker exec fikoyt-sunucu nc -zv mongodb 27017
```

## 🔧 Maintenance Komutları

### Günlük Kullanım
```bash
# Başlat
docker-compose up -d

# Durdur
docker-compose down

# Yeniden başlat
docker-compose restart

# Logları izle
docker-compose logs -f
```

### Build ve Cache
```bash
# Temiz build (cache kullanmadan)
docker-compose build --no-cache

# Build cache'i kullan (hızlı)
docker-compose build

# Image boyutunu kontrol et
docker images fikoyt-sunucu

# Unused images'ı sil
docker image prune -a
```

### Volume Yönetimi
```bash
# Volume'ları listele
docker volume ls | grep fikoyt

# FreeCAD log'unu oku
docker run --rm -v fikoyt_freecad_logs:/logs alpine cat /logs/freecad_api.log

# Go cache'i temizle (yavaş rebuild olacak)
docker volume rm fikoyt_go_build_cache

# Tüm volumes'ı temizle
docker-compose down -v
```

### Health Check
```bash
# Health status
docker ps --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}"

# Detaylı health info
docker inspect fikoyt-sunucu --format='{{json .State.Health}}' | python -m json.tool

# Manuel health check test
docker exec fikoyt-sunucu curl -f http://localhost:9000/health
```

## 🎯 Best Practices

### 1. Production Kullanımı
```bash
# .env dosyası kullan
echo "FLASK_DEBUG=False" > .env
echo "MONGO_HOST=mongodb" >> .env

# docker-compose.yml'de:
env_file:
  - .env
```

### 2. Backup
```bash
# Volume backup
docker run --rm -v fikoyt_freecad_logs:/data -v $(pwd):/backup alpine tar czf /backup/logs-backup.tar.gz /data

# Restore
docker run --rm -v fikoyt_freecad_logs:/data -v $(pwd):/backup alpine tar xzf /backup/logs-backup.tar.gz -C /
```

### 3. Monitoring
```bash
# Prometheus metrics (eğer varsa)
curl http://localhost:9000/metrics

# Docker stats
watch -n 1 docker stats fikoyt-sunucu
```

## 🆘 Troubleshooting Matrix

| Sorun | Kontrol | Çözüm |
|-------|---------|-------|
| Container başlamıyor | `docker logs fikoyt-sunucu` | start.sh hatalarını kontrol et |
| Health check fail | `docker inspect` | Go API çalışıyor mu kontrol et |
| MongoDB bağlanamıyor | `docker network inspect` | fikoyt_ag network'ünde mi? |
| Disk dolu | `docker system df` | `docker system prune -a` |
| Memory yüksek | `docker stats` | Resource limitleri artır |
| Python API yok | `docker exec ls /app/python_scripts` | Volume mount kontrolü |

## 📝 Configuration Örnekleri

### Minimal (Test için)
```yaml
services:
  fikoyt-sunucu:
    image: fikoyt-sunucu:latest
    ports:
      - "9000:9000"
    networks:
      - fikoyt_ag
```

### Production (Tam özellikli)
Mevcut `docker-compose.yml` üretim için hazır!

### Development (Hot reload)
```yaml
environment:
  - FLASK_DEBUG=True
  - AIR_ENABLED=true
volumes:
  - ./merkez.go-server.com:/app:cached
```

---

**Not**: Bu özellikler sayesinde artık sıkıntısız, stabil bir Docker ortamınız var! 🚀
