# Observation App - Docker Deployment Guide This guide provides a complete Docker-based deployment solution for the Observation App with automated database import and image handling. ## ๐Ÿ—๏ธ Architecture - **observation_app**: Next.js application container - **observation_db**: MongoDB database container - **observation_net**: Private network for container communication ## ๐Ÿ“‹ Prerequisites - Docker and Docker Compose installed - `.env` file configured - `public/` directory with required files - `public/db/` directory with JSON database files (optional) ## ๐Ÿš€ Quick Deployment ### 1. Prepare Environment Ensure you have: ```bash # .env file with configuration MONGODB_URI=mongodb://localhost/beanstalk # ... other environment variables # public directory structure public/ โ”œโ”€โ”€ observations/ # Image files โ”œโ”€โ”€ db/ # JSON database files โ”‚ โ”œโ”€โ”€ indicators.json โ”‚ โ”œโ”€โ”€ learning_areas.json โ”‚ โ””โ”€โ”€ observations.json โ””โ”€โ”€ ... # Other static files ``` ### 2. Deploy ```bash # Make deployment script executable chmod +x scripts/deploy.sh # Run deployment ./scripts/deploy.sh ``` The deployment script will: - โœ… Build and start containers - โœ… Wait for database readiness - โœ… Import JSON files from `public/db/` automatically - โœ… Copy images to container - โœ… Perform health checks ### 3. Access Services - **Application**: http://localhost:3000 - **Database**: `mongodb://admin:password123@localhost:27017` ## ๐Ÿ› ๏ธ Management Commands ### Container Management ```bash # View logs docker-compose logs -f # Stop services docker-compose down # Restart services docker-compose restart # Access application container docker-compose exec observation_app sh # Access database docker-compose exec observation_db mongosh ``` ### Database Operations ```bash # Import new JSON file curl -X POST -F "file=@data.json" http://localhost:3000/api/import-json # Export database docker-compose exec observation_db mongodump --uri="mongodb://admin:password123@localhost:27017/beanstalk?authSource=admin" --archive > backup.archive ``` ## ๐Ÿ“ Data Management ### Images - Place images in `public/observations/` - Automatically copied to container during deployment - Accessible via `/api/proxy-image` endpoint ### Database Files - Place JSON files in `public/db/` - Filename becomes collection name (e.g., `users.json` โ†’ `users` collection) - Automatically imported during deployment - Existing collections are dropped before import ## ๐Ÿ”ง Configuration ### Environment Variables ```bash # Database Connection MONGODB_URI=mongodb://admin:password123@observation_db:27017/beanstalk?authSource=admin # Application NODE_ENV=production ``` ### Docker Compose Settings - **MongoDB Version**: 6.0 - **Node.js Version**: 18 Alpine - **Ports**: App (3000), Database (27017) - **Volumes**: Persistent MongoDB data ## ๐Ÿ”„ Backup & Restore ### Create Backup ```bash chmod +x scripts/backup.sh ./scripts/backup.sh ``` Creates backup in `backups/YYYYMMDD_HHMMSS/` with: - MongoDB database archive - Image files - Configuration files - Automated restore script ### Restore from Backup ```bash cd backups/YYYYMMDD_HHMMSS/ ./restore.sh ``` ## ๐Ÿงน Cleanup ### Complete Cleanup ```bash chmod +x scripts/cleanup.sh ./scripts/cleanup.sh ``` Removes: - All containers - Application images - Unused networks - Optionally: Database volumes (with confirmation) ## ๐Ÿ” Troubleshooting ### Database Connection Issues ```bash # Check database status docker-compose exec observation_db mongosh --eval "db.adminCommand('ping')" # View database logs docker-compose logs observation_db ``` ### Application Issues ```bash # Check application health curl http://localhost:3000/api/health # View application logs docker-compose logs observation_app ``` ### Rebuild Containers ```bash # Force rebuild without cache docker-compose build --no-cache docker-compose up -d ``` ## ๐Ÿ“Š Monitoring ### Health Checks - Application: `/api/health` - Database: MongoDB ping command - Container status: `docker-compose ps` ### Logs ```bash # All services docker-compose logs -f # Specific service docker-compose logs -f observation_app docker-compose logs -f observation_db ``` ## ๐Ÿ”’ Security Notes - Default credentials are for development only - Change MongoDB passwords in production - Use environment variables for sensitive data - Consider using Docker secrets for production ## ๐Ÿš€ Production Considerations 1. **Security**: Update default passwords 2. **SSL**: Add HTTPS termination 3. **Backup**: Schedule regular backups 4. **Monitoring**: Add health monitoring 5. **Scaling**: Consider load balancer for multiple app instances