# Docker Containerization Guide

## Overview
This inventory management system has been containerized with Docker for easy deployment and scaling. The setup includes:
- Backend PHP service with Apache
- Frontend Vue.js service with Nginx
- **External database connectivity** (MySQL/PostgreSQL/etc.)
- Redis cache (optional)
- External volume mounts for uploads

## Quick Start

### Prerequisites
- Docker and Docker Compose installed
- At least 2GB RAM available
- Sufficient disk space for uploads
- **External database server** (MySQL 8.0+ recommended)

### 1. Environment Configuration
For external database setup, use the external database template:

```bash
cp .env.external .env
```

Edit the `.env` file with your external database configuration:

```bash
# External Database Configuration
DB_HOST=your-external-db-host
DB_PORT=3306
DB_NAME=inventory_db
DB_USER=your-db-username
DB_PASS=your-db-password

# Company Information
COMPANY_NAME=Your Company Name
COMPANY_ADDRESS=123 Business Street
COMPANY_CITY=Helsinki
COMPANY_POSTAL_CODE=00100
COMPANY_COUNTRY=Finland
COMPANY_PHONE=+358 123 456 789
COMPANY_EMAIL=info@yourcompany.com
COMPANY_Y_TUNNUS=1234567-8

# File Upload Configuration
UPLOAD_MAX_SIZE=10M
ALLOWED_FILE_TYPES=pdf,doc,docx,xls,xlsx,jpg,jpeg,png,gif
UPLOADS_PATH=./uploads

# Frontend Configuration
VUE_APP_API_URL=http://localhost:8080
```

### 2. Database Setup
Before starting the containers, ensure your external database is ready:

```sql
-- Create database and user (MySQL example)
CREATE DATABASE inventory_db;
CREATE USER 'your-db-username'@'%' IDENTIFIED BY 'your-db-password';
GRANT ALL PRIVILEGES ON inventory_db.* TO 'your-db-username'@'%';
FLUSH PRIVILEGES;
```

Import the database schema:
```bash
mysql -h your-external-db-host -u your-db-username -p inventory_db < database/init.sql
```

### 3. Build and Start Containers
```bash
docker-compose up -d --build
```

### 4. Access the Application
- Frontend: http://localhost:3000
- Backend API: http://localhost:8080
- Database: your-external-db-host:3306 (with your configured credentials)

## Configuration Details

### Environment Variables

#### Database Configuration
- `DB_HOST`: External database server hostname (required)
- `DB_PORT`: Database port (default: 3306)
- `DB_NAME`: Database name (default: inventory_db)
- `DB_USER`: Database username (required)
- `DB_PASS`: Database password (required)

#### Company Information
- `COMPANY_NAME`: Your company name
- `COMPANY_ADDRESS`: Company address
- `COMPANY_CITY`: Company city
- `COMPANY_POSTAL_CODE`: Postal code
- `COMPANY_COUNTRY`: Country
- `COMPANY_PHONE`: Phone number
- `COMPANY_EMAIL`: Email address
- `COMPANY_Y_TUNNUS`: Finnish business ID

#### File Upload Configuration
- `UPLOAD_MAX_SIZE`: Maximum file upload size (default: 10M)
- `ALLOWED_FILE_TYPES`: Allowed file extensions
- `UPLOADS_PATH`: External path for uploads volume mount

#### Frontend Configuration
- `VUE_APP_API_URL`: Backend API URL for frontend

### Volume Mounts

#### Uploads Directory
The uploads directory is mounted as an external volume to persist file uploads across container restarts:

```yaml
volumes:
  uploads_data:
    driver: local
    driver_opts:
      type: none
      o: bind
      device: ${UPLOADS_PATH:-./uploads}
```

#### Database Data
MySQL data is persisted in a Docker volume:

```yaml
volumes:
  mysql_data:
    driver: local
```

### Network Configuration
All services communicate through a dedicated Docker network:

```yaml
networks:
  inventory-network:
    driver: bridge
```

## Production Deployment

### Security Considerations
1. Change all default passwords
2. Use HTTPS in production (configure SSL certificates)
3. Restrict database access to internal network only
4. Regularly update containers and dependencies
5. Implement proper backup strategy for database and uploads

### Scaling
The architecture supports horizontal scaling:
- Frontend can be scaled by adding more Nginx containers behind a load balancer
- Backend can be scaled by adding more PHP containers
- Database can be moved to external managed service

### Monitoring
Consider adding:
- Health checks for all services
- Log aggregation
- Performance monitoring
- Backup automation

## Development

### Local Development
For development, you can run services individually:

```bash
# Start only database
docker-compose up -d mysql

# Start backend with live reload
docker-compose up -d --build backend

# Start frontend with live reload
cd frontend && npm run serve
```

### Debugging
View logs for specific services:

```bash
# Backend logs
docker-compose logs -f backend

# Frontend logs
docker-compose logs -f frontend

# Database logs
docker-compose logs -f mysql
```

### Database Management
Connect to the database:

```bash
docker-compose exec mysql mysql -u inventory_user -p inventory_db
```

## Troubleshooting

### Common Issues

#### Database Connection Failed
1. Check if MySQL container is running: `docker-compose ps`
2. Verify database credentials in `.env` file
3. Check database logs: `docker-compose logs mysql`

#### Upload Directory Not Working
1. Ensure uploads directory exists on host
2. Check permissions: `ls -la uploads/`
3. Verify volume mount in docker-compose.yml

#### Frontend Not Loading
1. Check if backend is accessible: `curl http://localhost:8080/api/company.php`
2. Verify API URL configuration
3. Check frontend logs: `docker-compose logs frontend`

### Performance Optimization
1. Enable Redis caching for frequently accessed data
2. Configure PHP OPcache for better performance
3. Use CDN for static assets
4. Implement database connection pooling

## Maintenance

### Updates
To update the application:

```bash
# Pull latest changes
git pull

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

### Backups
Regular backups should include:
1. Database dump: `docker-compose exec mysql mysqldump -u root -p inventory_db`
2. Uploads directory: `tar -czf uploads_backup.tar.gz uploads/`

### Cleanup
Remove unused containers and images:

```bash
docker system prune -a
docker volume prune
```

## Support
For issues and questions, refer to the application logs or contact your system administrator.
