Parcourir la source

Dockerize solution

svalavuo il y a 1 jour
Parent
commit
24205b90b8
2 fichiers modifiés avec 419 ajouts et 0 suppressions
  1. 356 0
      DOCKER_DEPLOYMENT.md
  2. 63 0
      build.sh

+ 356 - 0
DOCKER_DEPLOYMENT.md

@@ -0,0 +1,356 @@
+# Docker Deployment Guide
+
+## 🐳 Complete Inventory Management System in One Container
+
+This guide explains how to deploy the complete inventory management system using Docker and Docker Compose.
+
+## 📋 Prerequisites
+
+- Docker 20.10+ and Docker Compose 2.0+
+- At least 2GB RAM available
+- At least 10GB free disk space
+
+## 🚀 Quick Start
+
+### 1. Clone and Setup
+```bash
+git clone <repository-url>
+cd inventory
+```
+
+### 2. Configure Environment
+```bash
+# Copy environment file
+cp .env.example .env
+
+# Edit configuration (optional)
+nano .env
+```
+
+### 3. Build and Deploy
+```bash
+# Make build script executable
+chmod +x build.sh
+
+# Run the build script
+./build.sh
+```
+
+### 4. Access Application
+- **Frontend**: http://localhost
+- **API**: http://localhost/api
+- **Database**: localhost:3306 (user: inventory_db, pass: mDw(HF]Cub.UM2*7)
+
+## 🏗️ Architecture
+
+### Single Container Solution
+The Docker setup creates a single container that includes:
+- **Apache Web Server** with PHP 8.1
+- **Vue.js Frontend** (built and served)
+- **PHP Backend API** with all endpoints
+- **MySQL Database** (separate container)
+- **Redis Cache** (optional)
+
+### Container Structure
+```
+inventory-app (main container)
+├── Apache Web Server
+├── PHP Backend (/var/www/html/api)
+├── Vue.js Frontend (/var/www/html/frontend/dist)
+├── Uploads (/var/www/html/uploads)
+└── Configuration
+
+inventory-db (MySQL container)
+└── MySQL 8.0 Database
+
+inventory-redis (optional)
+└── Redis 7 Cache
+```
+
+## ⚙️ Configuration
+
+### Environment Variables (.env)
+```bash
+# Database Configuration
+DB_HOST=db                    # Docker service name
+DB_PORT=3306
+DB_NAME=inventory_db
+DB_USER=inventory_db
+DB_PASS=mDw(HF]Cub.UM2*7
+DB_ROOT_PASSWORD=rootpassword
+
+# Application Port
+APP_PORT=80                   # Web server port
+
+# Redis (optional)
+REDIS_PORT=6379
+
+# 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
+```
+
+## 🛠️ Docker Compose Services
+
+### inventory-app
+- **Purpose**: Main application container
+- **Image**: Built from Dockerfile
+- **Ports**: 80:80
+- **Features**: PHP backend + Vue.js frontend
+
+### db
+- **Purpose**: MySQL database
+- **Image**: mysql:8.0
+- **Ports**: 3306:3306
+- **Volume**: Persistent data storage
+- **Initialization**: Auto-loads database schema
+
+### redis (optional)
+- **Purpose**: Caching layer
+- **Image**: redis:7-alpine
+- **Ports**: 6379:6379
+- **Volume**: Persistent data storage
+
+## 📁 File Structure
+
+```
+inventory/
+├── Dockerfile                 # Multi-stage build (Node.js + PHP)
+├── docker-compose.yml         # Container orchestration
+├── docker/
+│   └── apache.conf           # Apache configuration
+├── backend/
+│   ├── api/                  # PHP API endpoints
+│   ├── config/               # Configuration files
+│   ├── models/               # PHP models
+│   └── migrate_complete.sql  # Database schema
+├── frontend/
+│   ├── src/                  # Vue.js source code
+│   ├── dist/                 # Built frontend (generated)
+│   └── package.json          # Node.js dependencies
+├── uploads/                  # File upload directory
+├── .env.example              # Environment template
+├── .env                      # Environment configuration
+└── build.sh                  # Build script
+```
+
+## 🔧 Management Commands
+
+### Start Application
+```bash
+docker-compose up -d
+```
+
+### Stop Application
+```bash
+docker-compose down
+```
+
+### View Logs
+```bash
+# All logs
+docker-compose logs
+
+# Specific service logs
+docker-compose logs inventory-app
+docker-compose logs db
+docker-compose logs redis
+
+# Follow logs
+docker-compose logs -f inventory-app
+```
+
+### Rebuild Application
+```bash
+# Force rebuild without cache
+docker-compose build --no-cache
+
+# Rebuild specific service
+docker-compose build inventory-app
+```
+
+### Database Management
+```bash
+# Access database shell
+docker-compose exec db mysql -u inventory_db -p
+
+# Backup database
+docker-compose exec db mysqldump -u inventory_db inventory_db > backup.sql
+
+# Restore database
+docker-compose exec -T db mysql -u inventory_db inventory_db < backup.sql
+```
+
+### Container Management
+```bash
+# List containers
+docker-compose ps
+
+# Access container shell
+docker-compose exec inventory-app bash
+
+# Restart specific service
+docker-compose restart inventory-app
+
+# Update environment variables
+docker-compose down
+# Edit .env file
+docker-compose up -d
+```
+
+## 🚨 Troubleshooting
+
+### Common Issues
+
+#### 1. Database Connection Failed
+```bash
+# Check database status
+docker-compose logs db
+
+# Restart database
+docker-compose restart db
+
+# Wait for database to be ready
+docker-compose exec db mysqladmin ping -h localhost
+```
+
+#### 2. Frontend Not Loading
+```bash
+# Check application logs
+docker-compose logs inventory-app
+
+# Rebuild frontend
+docker-compose build --no-cache inventory-app
+
+# Check Apache configuration
+docker-compose exec inventory-app apache2ctl -M
+```
+
+#### 3. File Upload Issues
+```bash
+# Check uploads directory permissions
+docker-compose exec inventory-app ls -la /var/www/html/uploads
+
+# Fix permissions
+docker-compose exec inventory-app chown -R www-data:www-data /var/www/html/uploads
+docker-compose exec inventory-app chmod -R 755 /var/www/html/uploads
+```
+
+#### 4. API Not Responding
+```bash
+# Test API endpoint
+curl http://localhost/api/company.php
+
+# Check Apache status
+docker-compose exec inventory-app service apache2 status
+
+# Restart Apache
+docker-compose exec inventory-app service apache2 restart
+```
+
+### Health Checks
+
+The application includes health checks:
+- **inventory-app**: Tests `/api/company.php` endpoint
+- **db**: Tests MySQL connectivity
+- **redis**: Tests Redis connectivity
+
+View health status:
+```bash
+docker-compose ps
+```
+
+## 🔒 Security Considerations
+
+### Production Deployment
+1. **Change Default Passwords**: Update database passwords in .env
+2. **Use HTTPS**: Configure SSL/TLS termination
+3. **Network Isolation**: Use custom networks
+4. **Resource Limits**: Set memory and CPU limits
+5. **Regular Updates**: Keep Docker images updated
+
+### Environment Security
+```bash
+# Secure .env file
+chmod 600 .env
+
+# Use secrets for sensitive data
+echo "DB_PASSWORD=your_secure_password" > .env.secret
+```
+
+## 📈 Performance Optimization
+
+### Production Optimizations
+1. **Enable Redis Caching**: Configure Redis for session storage
+2. **Database Optimization**: Use persistent connections
+3. **Frontend Caching**: Enable browser caching
+4. **Load Balancing**: Use multiple containers behind a load balancer
+
+### Monitoring
+```bash
+# Monitor resource usage
+docker stats
+
+# Monitor disk usage
+docker-compose exec inventory-app df -h
+
+# Monitor memory usage
+docker-compose exec inventory-app free -m
+```
+
+## 🔄 Updates and Maintenance
+
+### Updating Application
+```bash
+# Pull latest code
+git pull
+
+# Rebuild and restart
+docker-compose build --no-cache
+docker-compose up -d
+
+# Run database migrations if needed
+docker-compose exec db mysql -u inventory_db inventory_db < migration.sql
+```
+
+### Backup Strategy
+```bash
+# Backup database
+docker-compose exec db mysqldump -u inventory_db inventory_db > backup_$(date +%Y%m%d).sql
+
+# Backup uploads
+tar -czf uploads_$(date +%Y%m%d).tar.gz uploads/
+
+# Backup configuration
+cp .env .env.backup
+```
+
+## 📞 Support
+
+For issues with the Docker deployment:
+1. Check the troubleshooting section above
+2. Review container logs: `docker-compose logs`
+3. Verify environment configuration
+4. Check system resources (memory, disk space)
+
+## 🎉 Success!
+
+Your inventory management system is now running in Docker with:
+- ✅ Complete frontend and backend in one container
+- ✅ Automatic database setup and migration
+- ✅ File upload functionality
+- ✅ Timer management and work hours
+- ✅ Persistent data storage
+- ✅ Health monitoring
+- ✅ Easy deployment and management

+ 63 - 0
build.sh

@@ -0,0 +1,63 @@
+#!/bin/bash
+
+# Inventory Management System Docker Build Script
+# This script builds and deploys the complete inventory solution
+
+set -e
+
+echo "🚀 Building Inventory Management System..."
+
+# Check if Docker is installed
+if ! command -v docker &> /dev/null; then
+    echo "❌ Docker is not installed. Please install Docker first."
+    exit 1
+fi
+
+# Check if Docker Compose is installed
+if ! command -v docker-compose &> /dev/null; then
+    echo "❌ Docker Compose is not installed. Please install Docker Compose first."
+    exit 1
+fi
+
+# Check if .env file exists, if not copy from example
+if [ ! -f .env ]; then
+    echo "📝 Creating .env file from .env.example..."
+    cp .env.example .env
+    echo "⚠️  Please edit .env file with your configuration before running the application."
+fi
+
+# Create uploads directory if it doesn't exist
+if [ ! -d uploads ]; then
+    echo "📁 Creating uploads directory..."
+    mkdir -p uploads
+    chmod 755 uploads
+fi
+
+# Build and start the application
+echo "🔨 Building Docker containers..."
+docker-compose build
+
+echo "🚀 Starting application..."
+docker-compose up -d
+
+# Wait for database to be ready
+echo "⏳ Waiting for database to be ready..."
+sleep 30
+
+# Check if containers are running
+echo "🔍 Checking container status..."
+docker-compose ps
+
+# Show logs
+echo "📋 Showing application logs..."
+docker-compose logs inventory-app
+
+echo "✅ Build complete!"
+echo ""
+echo "🌐 Application is available at: http://localhost"
+echo "🔧 API endpoints are available at: http://localhost/api"
+echo "📊 Database is available at: localhost:3306"
+echo ""
+echo "📝 To stop the application: docker-compose down"
+echo "📝 To view logs: docker-compose logs -f"
+echo "📝 To rebuild: docker-compose build --no-cache"