# Docker Setup for Documents Merger

This project includes Docker configuration for easy deployment and development.

## Requirements

- Docker Engine 20.10+
- Docker Compose 1.29+

## Quick Start

1. **Build and run the container:**
   ```bash
   docker-compose up -d
   ```

2. **Access the application:**
   - Application: http://localhost:8000
   - API endpoints: http://localhost:8000/api/document/{id}/fields

## Docker Configuration

### PHP Version
- **PHP 7.4** with Apache web server
- Includes all required extensions:
  - GD (for image processing)
  - Imagick (for SVG to PNG conversion)
  - PDO/PDO_MySQL
  - Zip
  - OPcache

### Key Features
- Automatic composer dependency installation
- Optimized PHP settings for document processing:
  - Memory limit: 256MB
  - Upload max filesize: 20MB
  - Max execution time: 300 seconds
- Proper permissions for storage directories
- Apache mod_rewrite enabled

## Development Usage

### Start the container:
```bash
docker-compose up -d
```

### View logs:
```bash
docker-compose logs -f app
```

### Execute commands inside container:
```bash
# Run artisan commands
docker-compose exec app php artisan cache:clear

# Run tests
docker-compose exec app ./vendor/bin/phpunit

# Access container shell
docker-compose exec app bash
```

### Stop the container:
```bash
docker-compose down
```

## Production Build

For production, build the image directly:

```bash
# Build the image
docker build -t documents-merger:prod .

# Run the container
docker run -d \
  --name documents-merger \
  -p 80:80 \
  -v $(pwd)/.env:/var/www/html/.env \
  -v $(pwd)/storage/resources/documents:/var/www/html/storage/resources/documents \
  documents-merger:prod
```

## Environment Variables

Make sure to create a `.env` file based on `.env.example`:

```bash
cp .env.example .env
```

Key environment variables:
- `APP_ENV`: Set to `production` for production use
- `APP_DEBUG`: Set to `false` for production
- `APP_KEY`: Generate a secure key for production

## Volumes

The docker-compose setup mounts several volumes for development:
- Source code directories (app, config, routes, etc.)
- Storage directories for logs and documents
- Environment file

For production, consider only mounting:
- `.env` file
- `storage/resources/documents` (if templates change frequently)

## Troubleshooting

### Permission Issues
If you encounter permission errors:
```bash
docker-compose exec app chown -R www-data:www-data storage
docker-compose exec app chmod -R 775 storage
```

### Memory Issues
If processing large documents fails, increase PHP memory limit in Dockerfile:
```dockerfile
RUN echo "memory_limit=512M" > /usr/local/etc/php/conf.d/docker-php-memlimit.ini
```

### Imagick Installation Issues
If Imagick fails to install, try using a different version:
```dockerfile
RUN pecl install imagick-3.5.1
```

## Security Considerations

- Don't include sensitive data in the Docker image
- Use secrets management for production environments
- Regularly update base images for security patches
- Consider using multi-stage builds for smaller production images