Commit 4d104c8c authored by Blockchain-vn's avatar Blockchain-vn

refactor/db-docker

parent 02e43bce
......@@ -16,8 +16,15 @@ PROJECT_VERSION=1.0.0
# Database Configuration
# For MongoDB (recommended for this project)
# Option 1: MongoDB Atlas (cloud)
MONGODB_URI=mongodb+srv://username:password@cluster.mongodb.net/database_name?retryWrites=true&w=majority
# Option 2: Local MongoDB with Docker (development)
# MONGODB_URI=mongodb://trainee_user:trainee_password@localhost:27017/trainee-schedule?authSource=trainee-schedule
# Option 3: Local MongoDB with admin user
# MONGODB_URI=mongodb://admin:password123@localhost:27017/trainee-schedule?authSource=admin
# For PostgreSQL (legacy - not used in this project)
DB_HOST=localhost
DB_PORT=5432
......
# Docker MongoDB Setup Guide
## Quick Start
### 1. Start MongoDB with Docker
```bash
# Start MongoDB only
docker-compose --profile dev up mongodb -d
# Or start both MongoDB and app
docker-compose --profile dev up -d
```
### 2. Update .env file
Copy `.env.example` to `.env` and update the MongoDB URI:
```bash
cp .env.example .env
```
For local Docker MongoDB, use:
```env
MONGODB_URI=mongodb://trainee_user:trainee_password@localhost:27017/trainee-schedule?authSource=trainee-schedule
```
### 3. Start the application
```bash
# If not using Docker for the app
npm install
npm run dev
# Or using Docker for everything
docker-compose --profile dev up app-dev -d
```
## Docker Services
### MongoDB Service
- **Container Name**: `trainee-schedule-mongodb`
- **Port**: `27017` (host) -> `27017` (container)
- **Database**: `trainee-schedule`
- **Default User**: `trainee_user` / `trainee_password`
- **Admin User**: `admin` / `password123`
### App Services
- **Development**: `app-dev` profile
- **Production**: `app-prod` profile
## Useful Commands
### MongoDB Management
```bash
# View MongoDB logs
docker-compose logs mongodb
# Connect to MongoDB shell
docker exec -it trainee-schedule-mongodb mongosh
# Connect with admin user
docker exec -it trainee-schedule-mongodb mongosh -u admin -p password123 --authenticationDatabase admin
# Stop MongoDB
docker-compose stop mongodb
# Remove MongoDB container and data
docker-compose down -v
```
### Application Management
```bash
# View app logs
docker-compose logs app-dev
# Restart app
docker-compose restart app-dev
# Stop all services
docker-compose --profile dev down
```
## Database Initialization
The MongoDB container automatically:
1. Creates `trainee-schedule` database
2. Creates `trainee_user` with read/write permissions
3. Creates collections: `students`, `courses`, `schedules`, `bookings`
4. Sets up indexes for better performance
## Environment Variables
### MongoDB Connection Options
1. **MongoDB Atlas (Cloud)**:
```env
MONGODB_URI=mongodb+srv://username:password@cluster.mongodb.net/trainee-schedule?retryWrites=true&w=majority
```
2. **Local Docker (Recommended for Development)**:
```env
MONGODB_URI=mongodb://trainee_user:trainee_password@localhost:27017/trainee-schedule?authSource=trainee-schedule
```
3. **Local Docker with Admin**:
```env
MONGODB_URI=mongodb://admin:password123@localhost:27017/trainee-schedule?authSource=admin
```
## Troubleshooting
### Port Already in Use
```bash
# Check what's using port 27017
netstat -tulpn | grep 27017
# Kill the process
sudo kill -9 <PID>
```
### Connection Issues
1. Ensure MongoDB container is running:
```bash
docker ps | grep mongodb
```
2. Check container logs:
```bash
docker-compose logs mongodb
```
3. Verify .env MONGODB_URI matches container configuration
### Reset Database
```bash
# Stop and remove all data
docker-compose down -v
# Start fresh
docker-compose --profile dev up mongodb -d
```
## Development Workflow
1. **Start MongoDB**: `docker-compose --profile dev up mongodb -d`
2. **Start App**: `npm run dev`
3. **Test APIs**: Visit `http://localhost:3001/api-docs`
4. **Stop when done**: `docker-compose --profile dev down`
## Production Deployment
For production, use:
```bash
docker-compose --profile prod up -d
```
This starts both MongoDB and the production app with optimized settings.
version: "3.8"
services:
mongodb:
profiles: ["dev", "prod"]
image: mongo:7.0
container_name: trainee-schedule-mongodb
restart: unless-stopped
ports:
- "27017:27017"
environment:
- MONGO_INITDB_ROOT_USERNAME=admin
- MONGO_INITDB_ROOT_PASSWORD=password123
- MONGO_INITDB_DATABASE=trainee-schedule
volumes:
- mongodb_data:/data/db
- ./docker/mongodb/init:/docker-entrypoint-initdb.d
healthcheck:
test: ["CMD", "mongosh", "--eval", "db.adminCommand('ping')"]
interval: 30s
timeout: 10s
retries: 3
start_period: 40s
app-dev:
profiles: ["dev"]
build:
......@@ -48,3 +69,6 @@ services:
retries: 3
start_period: 40s
command: node dist/index.js
volumes:
mongodb_data:
// MongoDB initialization script
// This script runs when the container first starts
// Switch to the trainee-schedule database
db = db.getSiblingDB('trainee-schedule');
// Create application user with limited permissions
db.createUser({
user: 'trainee_user',
pwd: 'trainee_password',
roles: [
{
role: 'readWrite',
db: 'trainee-schedule'
}
]
});
// Create collections and indexes for better performance
db.createCollection('students');
db.students.createIndex({ email: 1 }, { unique: true });
db.students.createIndex({ courseId: 1 });
db.students.createIndex({ status: 1 });
db.createCollection('courses');
db.courses.createIndex({ code: 1 }, { unique: true });
db.courses.createIndex({ status: 1 });
db.courses.createIndex({ instructor: 1 });
db.createCollection('schedules');
db.schedules.createIndex({ courseId: 1 });
db.schedules.createIndex({ date: 1 });
db.schedules.createIndex({ status: 1 });
db.createCollection('bookings');
db.bookings.createIndex({ studentId: 1, scheduleId: 1 }, { unique: true });
db.bookings.createIndex({ status: 1 });
print('Database initialized successfully!');
print('Created user: trainee_user');
print('Created collections: students, courses, schedules, bookings');
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment