updated docker compose and env file

This commit is contained in:
Quality System Admin
2025-11-03 23:30:16 +02:00
parent 1cb54be01e
commit 9020f2c1cf
2 changed files with 139 additions and 0 deletions

View File

@@ -0,0 +1,139 @@
================================================================================
DOCKER ENVIRONMENT - READY FOR DEPLOYMENT
================================================================================
Date: $(date)
Project: Quality App (Trasabilitate)
Location: /srv/quality_app
================================================================================
CONFIGURATION FILES
================================================================================
✓ docker-compose.yml - 171 lines (simplified)
✓ .env - Complete configuration
✓ .env.example - Template for reference
✓ Dockerfile - Application container
✓ docker-entrypoint.sh - Startup script
✓ init-db.sql - Database initialization
================================================================================
ENVIRONMENT VARIABLES (.env)
================================================================================
Database:
DB_HOST=db
DB_PORT=3306
DB_NAME=trasabilitate
DB_USER=trasabilitate
DB_PASSWORD=Initial01!
MYSQL_ROOT_PASSWORD=rootpassword
Application:
APP_PORT=8781
FLASK_ENV=production
VERSION=1.0.0
SECRET_KEY=change-this-in-production
Gunicorn:
GUNICORN_WORKERS=(auto-calculated)
GUNICORN_TIMEOUT=1800
GUNICORN_WORKER_CLASS=sync
GUNICORN_MAX_REQUESTS=1000
Initialization (FIRST RUN ONLY):
INIT_DB=false
SEED_DB=false
Paths:
DB_DATA_PATH=/srv/quality_app/mariadb
LOGS_PATH=/srv/quality_app/logs
BACKUP_PATH=/srv/quality_app/backups
INSTANCE_PATH=/srv/quality_app/py_app/instance
Resources:
App: 2.0 CPU / 1G RAM
Database: 2.0 CPU / 1G RAM
================================================================================
DOCKER SERVICES
================================================================================
1. Database (quality-app-db)
- Image: mariadb:11.3
- Port: 3306
- Volume: /srv/quality_app/mariadb
- Health check: Enabled
2. Application (quality-app)
- Image: trasabilitate-quality-app:1.0.0
- Port: 8781
- Volumes: logs, backups, instance
- Health check: Enabled
Network: quality-app-network (172.20.0.0/16)
================================================================================
REQUIRED DIRECTORIES (ALL EXIST)
================================================================================
✓ /srv/quality_app/mariadb - Database storage
✓ /srv/quality_app/logs - Application logs
✓ /srv/quality_app/backups - Database backups
✓ /srv/quality_app/py_app/instance - Config files
================================================================================
DEPLOYMENT COMMANDS
================================================================================
First Time Setup:
1. Edit .env and set:
INIT_DB=true
SEED_DB=true
SECRET_KEY=<your-secure-key>
2. Build and start:
docker compose up -d --build
3. Watch logs:
docker compose logs -f web
4. After successful start, edit .env:
INIT_DB=false
SEED_DB=false
5. Restart:
docker compose restart web
Normal Operations:
- Start: docker compose up -d
- Stop: docker compose down
- Restart: docker compose restart
- Logs: docker compose logs -f
- Status: docker compose ps
================================================================================
SECURITY CHECKLIST
================================================================================
⚠ BEFORE PRODUCTION:
[ ] Change SECRET_KEY in .env
[ ] Change MYSQL_ROOT_PASSWORD in .env
[ ] Change DB_PASSWORD in .env
[ ] Set INIT_DB=false after first run
[ ] Set SEED_DB=false after first run
[ ] Review firewall rules
[ ] Set up SSL/TLS certificates
[ ] Configure backup schedule
[ ] Test restore procedures
================================================================================
VALIDATION STATUS
================================================================================
✓ Docker Compose configuration valid
✓ All required directories exist
✓ All environment variables set
✓ Network configuration correct
✓ Volume mappings correct
✓ Health checks configured
✓ Resource limits defined
================================================================================
READY FOR DEPLOYMENT ✓
================================================================================

View File

@@ -0,0 +1,314 @@
# Docker Compose - Quick Reference
## Simplified Structure
The Docker Compose configuration has been simplified with most settings moved to the `.env` file for easier management.
## File Structure
```
quality_app/
├── docker-compose.yml # Main Docker configuration (171 lines, simplified)
├── .env.example # Template with all available settings
├── .env # Your configuration (copy from .env.example)
├── Dockerfile # Application container definition
├── docker-entrypoint.sh # Container startup script
└── init-db.sql # Database initialization
```
## Quick Start
### 1. Initial Setup
```bash
# Navigate to project directory
cd /srv/quality_app
# Create .env file from template
cp .env.example .env
# Edit .env with your settings
nano .env
```
### 2. Configure .env File
**Required changes for first deployment:**
```bash
# Set these to true for first run only
INIT_DB=true
SEED_DB=true
# Change these in production
SECRET_KEY=your-secure-random-key-here
MYSQL_ROOT_PASSWORD=your-secure-root-password
DB_PASSWORD=your-secure-db-password
```
### 3. Create Required Directories
```bash
sudo mkdir -p /srv/quality_app/{mariadb,logs,backups}
sudo chown -R $USER:$USER /srv/quality_app
```
### 4. Start Services
```bash
# Start in detached mode
docker-compose up -d
# Watch logs
docker-compose logs -f web
```
### 5. After First Successful Start
```bash
# Edit .env and set:
INIT_DB=false
SEED_DB=false
# Restart to apply changes
docker-compose restart web
```
## Common Commands
### Service Management
```bash
# Start services
docker-compose up -d
# Stop services
docker-compose down
# Restart specific service
docker-compose restart web
docker-compose restart db
# View service status
docker-compose ps
# Remove all containers and volumes
docker-compose down -v
```
### Logs and Monitoring
```bash
# Follow all logs
docker-compose logs -f
# Follow specific service logs
docker-compose logs -f web
docker-compose logs -f db
# View last 100 lines
docker-compose logs --tail=100 web
# Check resource usage
docker stats quality-app quality-app-db
```
### Updates and Rebuilds
```bash
# Rebuild after code changes
docker-compose up -d --build
# Pull latest images
docker-compose pull
# Rebuild specific service
docker-compose up -d --build web
```
### Database Operations
```bash
# Access database CLI
docker-compose exec db mysql -u trasabilitate -p trasabilitate
# Backup database
docker-compose exec db mysqldump -u root -p trasabilitate > backup.sql
# Restore database
docker-compose exec -T db mysql -u root -p trasabilitate < backup.sql
# View database logs
docker-compose logs db
```
### Container Access
```bash
# Access web application shell
docker-compose exec web bash
# Access database shell
docker-compose exec db bash
# Run one-off command
docker-compose exec web python -c "print('Hello')"
```
## Environment Variables Reference
### Critical Settings (.env)
| Variable | Default | Description |
|----------|---------|-------------|
| `APP_PORT` | 8781 | Application port |
| `DB_PASSWORD` | Initial01! | Database password |
| `SECRET_KEY` | change-this | Flask secret key |
| `INIT_DB` | false | Initialize database on startup |
| `SEED_DB` | false | Seed default data on startup |
### Volume Paths
| Variable | Default | Purpose |
|----------|---------|---------|
| `DB_DATA_PATH` | /srv/quality_app/mariadb | Database files |
| `LOGS_PATH` | /srv/quality_app/logs | Application logs |
| `BACKUP_PATH` | /srv/quality_app/backups | Database backups |
| `INSTANCE_PATH` | /srv/quality_app/py_app/instance | Config files |
### Performance Tuning
| Variable | Default | Description |
|----------|---------|-------------|
| `GUNICORN_WORKERS` | auto | Number of workers |
| `GUNICORN_TIMEOUT` | 1800 | Request timeout (seconds) |
| `MYSQL_BUFFER_POOL` | 256M | Database buffer size |
| `MYSQL_MAX_CONNECTIONS` | 150 | Max DB connections |
| `APP_CPU_LIMIT` | 2.0 | CPU limit for app |
| `APP_MEMORY_LIMIT` | 1G | Memory limit for app |
## Configuration Changes
To change configuration:
1. Edit `.env` file
2. Restart affected service:
```bash
docker-compose restart web
# or
docker-compose restart db
```
### When to Restart vs Rebuild
**Restart only** (changes in .env):
- Environment variables
- Resource limits
- Port mappings
**Rebuild required** (code/Dockerfile changes):
```bash
docker-compose up -d --build
```
## Troubleshooting
### Application won't start
```bash
# Check logs
docker-compose logs web
# Check database health
docker-compose ps
docker-compose exec db mysqladmin ping -u root -p
# Verify .env file
cat .env | grep -v "^#" | grep -v "^$"
```
### Database connection issues
```bash
# Check database is running
docker-compose ps db
# Test database connection
docker-compose exec web python -c "
import mysql.connector
conn = mysql.connector.connect(
host='db', user='trasabilitate',
password='Initial01!', database='trasabilitate'
)
print('Connected OK')
"
```
### Port already in use
```bash
# Check what's using the port
sudo netstat -tlnp | grep 8781
# Change APP_PORT in .env
echo "APP_PORT=8782" >> .env
docker-compose up -d
```
### Reset everything
```bash
# Stop and remove all
docker-compose down -v
# Remove data (CAUTION: destroys database!)
sudo rm -rf /srv/quality_app/mariadb/*
# Restart fresh
INIT_DB=true SEED_DB=true docker-compose up -d
```
## Production Checklist
Before deploying to production:
- [ ] Change `SECRET_KEY` in .env
- [ ] Change `MYSQL_ROOT_PASSWORD` in .env
- [ ] Change `DB_PASSWORD` in .env
- [ ] Set `INIT_DB=false` after first run
- [ ] Set `SEED_DB=false` after first run
- [ ] Set `FLASK_ENV=production`
- [ ] Verify backup paths are correct
- [ ] Test backup and restore procedures
- [ ] Set up external monitoring
- [ ] Configure firewall rules
- [ ] Set up SSL/TLS certificates
- [ ] Review resource limits
- [ ] Set up log rotation
## Comparison: Before vs After
### Before (242 lines)
- Many inline default values
- Extensive comments in docker-compose.yml
- Hard to find and change settings
- Difficult to maintain multiple environments
### After (171 lines)
- Clean, readable docker-compose.yml (29% reduction)
- All settings in .env file
- Easy to customize per environment
- Simple to version control (just .env.example)
- Better separation of concerns
## Related Documentation
- [PRODUCTION_STARTUP_GUIDE.md](./documentation/PRODUCTION_STARTUP_GUIDE.md) - Application management
- [DATABASE_BACKUP_GUIDE.md](./documentation/DATABASE_BACKUP_GUIDE.md) - Backup procedures
- [DATABASE_RESTORE_GUIDE.md](./documentation/DATABASE_RESTORE_GUIDE.md) - Restore procedures
- [DATABASE_STRUCTURE.md](./documentation/DATABASE_STRUCTURE.md) - Database schema
---
**Last Updated**: November 3, 2025
**Docker Compose Version**: 3.8
**Configuration Style**: Environment-based (simplified)