Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
T
trainee-schedule
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Đoàn Vũ Bình Dương
trainee-schedule
Commits
c0781098
Commit
c0781098
authored
Apr 10, 2026
by
Đoàn Vũ Bình Dương
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'refactor/db-docker' into 'develop'
refactor/db-docker See merge request
!1
parents
02e43bce
4d104c8c
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
227 additions
and
0 deletions
+227
-0
.env.example
.env.example
+7
-0
DOCKER_SETUP.md
DOCKER_SETUP.md
+155
-0
docker-compose.yml
docker-compose.yml
+24
-0
01-init-db.js
docker/mongodb/init/01-init-db.js
+41
-0
No files found.
.env.example
View file @
c0781098
...
...
@@ -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_SETUP.md
0 → 100644
View file @
c0781098
# 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.
docker-compose.yml
View file @
c0781098
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
:
docker/mongodb/init/01-init-db.js
0 → 100644
View file @
c0781098
// 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'
);
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment