# Docker Quick Reference

## Essential Commands

### Initial Setup
```bash
# 1. Navigate to project
cd /Users/matthias/work/feuerwehr_dashboard

# 2. Validate Docker setup
./docker-validate.sh

# 3. Configure environment
cp .env.example .env
# Edit .env and set:
#   POSTGRES_PASSWORD=your_secure_password
#   JWT_SECRET=your_jwt_secret_min_32_chars

# 4. Test Docker builds (optional)
./docker-test.sh
```

### Start Application
```bash
# Build and start all services
docker-compose up -d

# View logs during startup
docker-compose logs -f

# Check service health
docker-compose ps
```

### Access Services
- Frontend: http://localhost:80
- Backend API: http://localhost:3000
- PostgreSQL: localhost:5432

### Manage Services
```bash
# Stop services (keeps data)
docker-compose stop

# Start stopped services
docker-compose start

# Restart all services
docker-compose restart

# Restart specific service
docker-compose restart backend

# Stop and remove containers (keeps data)
docker-compose down

# Stop, remove containers and volumes (DELETE DATA)
docker-compose down -v
```

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

# Specific service
docker-compose logs -f backend
docker-compose logs -f frontend
docker-compose logs -f postgres

# Last 100 lines
docker-compose logs --tail=100 backend

# Since specific time
docker-compose logs --since 2024-01-01T00:00:00
```

### Check Status
```bash
# Service status and health
docker-compose ps

# Container resource usage
docker stats

# Disk usage
docker system df
```

### Database Operations
```bash
# Access PostgreSQL CLI
docker-compose exec postgres psql -U prod_user -d feuerwehr_prod

# Create backup
docker-compose exec postgres pg_dump -U prod_user feuerwehr_prod > backup.sql

# Restore backup
cat backup.sql | docker-compose exec -T postgres psql -U prod_user feuerwehr_prod

# View database logs
docker-compose logs -f postgres
```

### Rebuild Services
```bash
# Rebuild all images
docker-compose build

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

# Rebuild specific service
docker-compose build backend

# Rebuild and restart
docker-compose up -d --build
```

### Execute Commands in Containers
```bash
# Backend shell
docker-compose exec backend sh

# Frontend/Nginx shell
docker-compose exec frontend sh

# Run command in backend
docker-compose exec backend node -v
docker-compose exec backend npm list

# View backend environment
docker-compose exec backend env
```

### Troubleshooting
```bash
# View full configuration
docker-compose config

# Validate docker-compose.yml
docker-compose config --quiet

# Remove all stopped containers
docker container prune

# Remove unused images
docker image prune

# Remove unused volumes
docker volume prune

# Remove everything (CAUTION!)
docker system prune -a
```

### Development Workflow
```bash
# 1. Make code changes in backend/frontend
# 2. Rebuild affected service
docker-compose build backend
# 3. Restart service
docker-compose up -d backend
# 4. Check logs
docker-compose logs -f backend
```

### Health Checks
```bash
# Check backend health
curl http://localhost:3000/health

# Check frontend health
curl http://localhost:80/health

# Check all services health
docker-compose ps
```

### Environment Variables
```bash
# View service environment
docker-compose exec backend env

# Override environment variable
BACKEND_PORT=4000 docker-compose up -d

# Use different env file
docker-compose --env-file .env.production up -d
```

## Build Individual Images

### Backend
```bash
cd backend
docker build -t feuerwehr-backend:latest .
docker run -p 3000:3000 \
  -e DATABASE_URL=postgresql://user:pass@postgres:5432/db \
  -e JWT_SECRET=your_secret \
  feuerwehr-backend:latest
```

### Frontend
```bash
cd frontend
docker build \
  --build-arg VITE_API_URL=http://localhost:3000 \
  -t feuerwehr-frontend:latest .
docker run -p 80:80 feuerwehr-frontend:latest
```

## Production Deployment

### Using Docker Compose
```bash
# 1. Set production environment variables
export POSTGRES_PASSWORD="secure_production_password"
export JWT_SECRET="secure_jwt_secret_min_32_chars"
export CORS_ORIGIN="https://feuerwehr-rems.at"
export VITE_API_URL="https://start.feuerwehr-rems.at"

# 2. Build and start
docker-compose up -d

# 3. Monitor
docker-compose logs -f
```

### Using Container Registry
```bash
# Tag images
docker tag feuerwehr-backend:latest registry.example.com/feuerwehr-backend:v1.0.0
docker tag feuerwehr-frontend:latest registry.example.com/feuerwehr-frontend:v1.0.0

# Push to registry
docker push registry.example.com/feuerwehr-backend:v1.0.0
docker push registry.example.com/feuerwehr-frontend:v1.0.0

# Pull on production server
docker pull registry.example.com/feuerwehr-backend:v1.0.0
docker pull registry.example.com/feuerwehr-frontend:v1.0.0

# Run with docker-compose
docker-compose up -d
```

## Monitoring

### Real-time Stats
```bash
# All containers
docker stats

# Specific container
docker stats feuerwehr_backend_prod
```

### Inspect Containers
```bash
# Container details
docker inspect feuerwehr_backend_prod

# Container IP address
docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' feuerwehr_backend_prod

# Container logs location
docker inspect --format='{{.LogPath}}' feuerwehr_backend_prod
```

## Cleanup

### Remove Test Images
```bash
docker rmi feuerwehr-backend-test:latest
docker rmi feuerwehr-frontend-test:latest
```

### Clean Build Cache
```bash
docker builder prune
```

### Remove Unused Resources
```bash
# Remove stopped containers
docker container prune -f

# Remove unused images
docker image prune -f

# Remove unused volumes
docker volume prune -f

# Remove unused networks
docker network prune -f

# Remove everything unused
docker system prune -af
```

## Security

### Check for Vulnerabilities
```bash
# Scan backend image
docker scan feuerwehr-backend:latest

# Scan frontend image
docker scan feuerwehr-frontend:latest
```

### Update Base Images
```bash
# Pull latest base images
docker pull node:20-alpine
docker pull nginx:alpine
docker pull postgres:16-alpine

# Rebuild with new base images
docker-compose build --no-cache
docker-compose up -d
```

## Performance Tuning

### Resource Limits
Add to docker-compose.yml:
```yaml
services:
  backend:
    deploy:
      resources:
        limits:
          cpus: '1.0'
          memory: 512M
        reservations:
          cpus: '0.5'
          memory: 256M
```

### Check Resource Usage
```bash
docker stats --no-stream
```

## Common Issues

### Port Already in Use
```bash
# Find process using port
lsof -i :3000

# Kill process
kill -9 <PID>

# Or use different port
BACKEND_PORT=3001 docker-compose up -d
```

### Database Connection Failed
```bash
# Check postgres is healthy
docker-compose ps postgres

# View postgres logs
docker-compose logs postgres

# Restart postgres
docker-compose restart postgres
```

### Container Crashes
```bash
# View crash logs
docker-compose logs backend

# Inspect container
docker inspect feuerwehr_backend_prod

# Check health
docker inspect --format='{{.State.Health.Status}}' feuerwehr_backend_prod
```

## References

- [Docker Setup Documentation](DOCKER_SETUP.md)
- [Summary Document](SUMMARY.md)
- [Backend Dockerfile](backend/Dockerfile)
- [Frontend Dockerfile](frontend/Dockerfile)
- [Nginx Configuration](frontend/nginx.conf)
- [Docker Compose File](docker-compose.yml)
