Explorar el Código

Modifications to Docker creation

svalavuo hace 1 día
padre
commit
33a9896179
Se han modificado 4 ficheros con 73 adiciones y 81 borrados
  1. 4 5
      .env.example
  2. 64 33
      DOCKER_DEPLOYMENT.md
  3. 1 5
      build.sh
  4. 4 38
      docker-compose.yml

+ 4 - 5
.env.example

@@ -1,10 +1,9 @@
-# Database Configuration (Docker MySQL)
-DB_HOST=db
+# Database Configuration (External Database)
+DB_HOST=your-external-db-host
 DB_PORT=3306
 DB_NAME=inventory_db
-DB_USER=inventory_db
-DB_PASS=mDw(HF]Cub.UM2*7
-DB_ROOT_PASSWORD=rootpassword
+DB_USER=your-db-username
+DB_PASS=your-db-password
 
 # Port Configuration
 APP_PORT=80

+ 64 - 33
DOCKER_DEPLOYMENT.md

@@ -39,7 +39,7 @@ chmod +x build.sh
 ### 4. Access Application
 - **Frontend**: http://localhost
 - **API**: http://localhost/api
-- **Database**: localhost:3306 (user: inventory_db, pass: mDw(HF]Cub.UM2*7)
+- **Database**: External database (configured in .env)
 
 ## 🏗️ Architecture
 
@@ -48,7 +48,7 @@ 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)
+- **External Database** (user-provided MySQL/MariaDB)
 - **Redis Cache** (optional)
 
 ### Container Structure
@@ -60,24 +60,23 @@ inventory-app (main container)
 ├── Uploads (/var/www/html/uploads)
 └── Configuration
 
-inventory-db (MySQL container)
-└── MySQL 8.0 Database
-
 inventory-redis (optional)
 └── Redis 7 Cache
+
+External Database (user-provided)
+└── MySQL/MariaDB Database
 ```
 
 ## ⚙️ Configuration
 
 ### Environment Variables (.env)
 ```bash
-# Database Configuration
-DB_HOST=db                    # Docker service name
+# Database Configuration (External)
+DB_HOST=your-external-db-host # Your database server
 DB_PORT=3306
 DB_NAME=inventory_db
-DB_USER=inventory_db
-DB_PASS=mDw(HF]Cub.UM2*7
-DB_ROOT_PASSWORD=rootpassword
+DB_USER=your-db-username
+DB_PASS=your-db-password
 
 # Application Port
 APP_PORT=80                   # Web server port
@@ -108,13 +107,7 @@ UPLOADS_PATH=./uploads
 - **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
+- **Database**: Connects to external database via environment variables
 
 ### redis (optional)
 - **Purpose**: Caching layer
@@ -122,6 +115,32 @@ UPLOADS_PATH=./uploads
 - **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
 
 ```
@@ -134,7 +153,7 @@ inventory/
 │   ├── api/                  # PHP API endpoints
 │   ├── config/               # Configuration files
 │   ├── models/               # PHP models
-│   └── migrate_complete.sql  # Database schema
+│   └── migrate_complete.sql  # Database schema (for external DB)
 ├── frontend/
 │   ├── src/                  # Vue.js source code
 │   ├── dist/                 # Built frontend (generated)
@@ -182,14 +201,17 @@ docker-compose build inventory-app
 
 ### Database Management
 ```bash
-# Access database shell
-docker-compose exec db mysql -u inventory_db -p
+# Access external database
+mysql -h your-external-db-host -u your-db-username -p inventory_db
 
-# Backup database
-docker-compose exec db mysqldump -u inventory_db inventory_db > backup.sql
+# 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
 
-# Restore database
-docker-compose exec -T db mysql -u inventory_db inventory_db < backup.sql
+# Test connection from container
+docker-compose exec inventory-app php -r "echo 'Testing DB connection...';"
 ```
 
 ### Container Management
@@ -215,14 +237,24 @@ docker-compose up -d
 
 #### 1. Database Connection Failed
 ```bash
-# Check database status
-docker-compose logs db
-
-# Restart database
-docker-compose restart db
+# Check application logs
+docker-compose logs inventory-app
 
-# Wait for database to be ready
-docker-compose exec db mysqladmin ping -h localhost
+# 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
@@ -263,8 +295,7 @@ docker-compose exec inventory-app service apache2 restart
 
 The application includes health checks:
 - **inventory-app**: Tests `/api/company.php` endpoint
-- **db**: Tests MySQL connectivity
-- **redis**: Tests Redis connectivity
+- **redis**: Tests Redis connectivity (optional)
 
 View health status:
 ```bash

+ 1 - 5
build.sh

@@ -40,10 +40,6 @@ 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
@@ -56,7 +52,7 @@ 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 "⚠️  Make sure your external database is accessible and configured in .env"
 echo ""
 echo "📝 To stop the application: docker-compose down"
 echo "📝 To view logs: docker-compose logs -f"

+ 4 - 38
docker-compose.yml

@@ -10,11 +10,11 @@ services:
     ports:
       - "${APP_PORT:-80}:80"
     environment:
-      - DB_HOST=${DB_HOST:-db}
+      - DB_HOST=${DB_HOST}
       - DB_PORT=${DB_PORT:-3306}
-      - DB_NAME=${DB_NAME:-inventory_db}
-      - DB_USER=${DB_USER:-inventory_db}
-      - DB_PASS=${DB_PASS:-mDw(HF]Cub.UM2*7}
+      - DB_NAME=${DB_NAME}
+      - DB_USER=${DB_USER}
+      - DB_PASS=${DB_PASS}
       - COMPANY_NAME=${COMPANY_NAME:-Inventory Management}
       - COMPANY_ADDRESS=${COMPANY_ADDRESS:-123 Business St}
       - COMPANY_CITY=${COMPANY_CITY:-Helsinki}
@@ -27,8 +27,6 @@ services:
       - ALLOWED_FILE_TYPES=${ALLOWED_FILE_TYPES:-pdf,doc,docx,xls,xlsx,jpg,jpeg,png,gif}
     volumes:
       - uploads_data:/var/www/html/uploads
-    networks:
-      - inventory-network
     restart: unless-stopped
     healthcheck:
       test: ["CMD", "curl", "-f", "http://localhost/api/company.php"]
@@ -37,30 +35,6 @@ services:
       retries: 3
       start_period: 40s
 
-  # MySQL Database Service
-  db:
-    image: mysql:8.0
-    container_name: inventory-db
-    ports:
-      - "${DB_PORT:-3306}:3306"
-    environment:
-      - MYSQL_ROOT_PASSWORD=${DB_ROOT_PASSWORD:-rootpassword}
-      - MYSQL_DATABASE=${DB_NAME:-inventory_db}
-      - MYSQL_USER=${DB_USER:-inventory_db}
-      - MYSQL_PASSWORD=${DB_PASS:-mDw(HF]Cub.UM2*7}
-    volumes:
-      - mysql_data:/var/lib/mysql
-      - ./backend/migrate_complete.sql:/docker-entrypoint-initdb.d/init.sql
-    networks:
-      - inventory-network
-    restart: unless-stopped
-    healthcheck:
-      test: ["CMD", "mysqladmin", "ping", "-h", "localhost"]
-      interval: 30s
-      timeout: 10s
-      retries: 3
-      start_period: 30s
-
   # Redis Cache Service (Optional)
   redis:
     image: redis:7-alpine
@@ -69,14 +43,10 @@ services:
       - "${REDIS_PORT:-6379}:6379"
     volumes:
       - redis_data:/data
-    networks:
-      - inventory-network
     restart: unless-stopped
     command: redis-server --appendonly yes
 
 volumes:
-  mysql_data:
-    driver: local
   redis_data:
     driver: local
   uploads_data:
@@ -85,7 +55,3 @@ volumes:
       type: none
       o: bind
       device: ${UPLOADS_PATH:-./uploads}
-
-networks:
-  inventory-network:
-    driver: bridge