DOCKER_DEPLOYMENT.md 12 KB

Docker Deployment Guide

๐Ÿณ Complete Inventory Management System with Docker

This guide explains how to deploy the complete inventory management system using Docker and Docker Compose with two deployment options.

๐Ÿ“‹ Prerequisites

  • Docker 20.10+ and Docker Compose 2.0+
  • At least 2GB RAM available (4GB recommended for full setup)
  • At least 10GB free disk space

๐Ÿš€ Deployment Options

Option 1: Basic Setup (External Database)

Perfect for production environments with existing database infrastructure.

1. Clone and Setup

git clone <repository-url>
cd inventory

2. Configure Environment

# Copy and edit environment file
cp backend/.env.local backend/.env.local
nano backend/.env.local

Configure your external database:

DB_HOST=your-external-db-host
DB_PORT=3306
DB_NAME=inventory_db
DB_USER=your-username
DB_PASS=your-password

3. Deploy

docker-compose up -d

4. Access Application

Option 2: Full Setup (Built-in Database + Cache)

Perfect for development, testing, or all-in-one deployments.

1. Clone and Setup

git clone <repository-url>
cd inventory

2. Deploy with Built-in Services

docker-compose -f docker-compose-with-services.yml --env-file .env.with-services up -d

3. Access Application

4. Default Credentials

  • Database User: inventory_user / inventory_password
  • Database Root: root / root_password
  • Redis: Password protected with redis_password

๐Ÿ—๏ธ Architecture

Basic Setup Architecture

Single container solution that includes:

  • Apache Web Server with PHP 8.1
  • Vue.js Frontend (built and served)
  • PHP Backend API with all endpoints
  • External Database Connectivity

Full Setup Architecture

Multi-container solution that includes:

  • inventory-app: Complete application (PHP + Apache + Vue.js)
  • mariadb: MariaDB 10.11 database server
  • redis: Redis 7 cache server
  • Persistent Volumes: Database, Redis, and uploads storage

    Container Structure

Basic Setup Structure

inventory-app (single container)
โ”œโ”€โ”€ Apache Web Server
โ”œโ”€โ”€ PHP Backend (/var/www/html/api)
โ”œโ”€โ”€ Vue.js Frontend (/var/www/html/frontend/dist)
โ”œโ”€โ”€ Uploads (/var/www/html/uploads)
โ””โ”€โ”€ External Database Connection

Full Setup 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)

inventory-mariadb (database container)
โ”œโ”€โ”€ MariaDB 10.11
โ”œโ”€โ”€ Auto-initialized Database
โ””โ”€โ”€ Persistent Storage

inventory-redis (cache container)
โ”œโ”€โ”€ Redis 7
โ”œโ”€โ”€ Password Protected
โ””โ”€โ”€ Persistent Storage

โš™๏ธ Configuration

Environment Variables

Basic Setup (.env.local)

# Database Configuration (External)
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

Full Setup (.env.with-services)

# Database Configuration (Built-in)
DB_HOST=mariadb
DB_PORT=3306
DB_NAME=inventory_db
DB_USER=inventory_user
DB_PASS=inventory_password
MYSQL_ROOT_PASSWORD=root_password

# Redis Configuration (Built-in)
REDIS_HOST=redis
REDIS_PORT=6379
REDIS_PASSWORD=redis_password

# Frontend Configuration
FRONTEND_PORT=80
MARIADB_PORT=3306
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
- **Database**: Connects to external database via environment variables

### redis (optional)
- **Purpose**: Caching layer
- **Image**: redis:7-alpine
- **Ports**: 6379:6379
- **Volume**: Persistent data storage

## ๐Ÿ—„๏ธ External Database Setup

### Database Requirements
- **MySQL 8.0+** or **MariaDB 10.5+**
- **Network Access**: Container must be able to reach database host
- **Schema**: Run `backend/migrate_complete.sql` to create database schema
- **Permissions**: User needs CREATE, SELECT, INSERT, UPDATE, DELETE privileges

### Database Setup Steps

bash

1. Create database

mysql -u root -p -e "CREATE DATABASE inventory_db;"

2. Create user

mysql -u root -p -e "CREATE USER 'inventory_db'@'%' IDENTIFIED BY 'your_password';"

3. Grant permissions

mysql -u root -p -e "GRANT ALL PRIVILEGES ON inventory_db.* TO 'inventory_db'@'%';"

4. Import schema

mysql -u inventory_db -p inventory_db < backend/migrate_complete.sql

5. Flush privileges

mysql -u root -p -e "FLUSH PRIVILEGES;"


## ๐Ÿ“ 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 (for external DB) โ”œโ”€โ”€ 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 external database

mysql -h your-external-db-host -u your-db-username -p inventory_db

Backup external database

mysqldump -h your-external-db-host -u your-db-username inventory_db > backup.sql

Restore external database

mysql -h your-external-db-host -u your-db-username inventory_db < backup.sql

Test connection from container

docker-compose exec inventory-app php -r "echo 'Testing DB connection...';"


### 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 application logs

docker-compose logs inventory-app

Test database connectivity from container

docker-compose exec inventory-app ping your-external-db-host

Test database connection manually

docker-compose exec inventory-app php -r " try {

\$pdo = new PDO('mysql:host=your-external-db-host;dbname=inventory_db', 'your-db-username', 'your-db-password');
echo 'Database connection successful';

} catch (Exception \$e) {

echo 'Database connection failed: ' . \$e->getMessage();

} "

Verify database schema exists

mysql -h your-external-db-host -u your-db-username -p inventory_db -e "SHOW TABLES;"


#### 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
- **redis**: Tests Redis connectivity (optional)

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 inventorydb > 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