This document provides comprehensive instructions for deploying the Learning Platform using Docker Compose. The platform is a Next.js application with a MongoDB database, Prisma ORM, and NextAuth for authentication.
Before deploying the application, ensure the following tools are installed on your system:
- Docker: Install Docker
- Docker Compose: Install Docker Compose
Clone the Learning Platform repository to your local machine:
git clone https://github.com/your-repo/learning-platform.git
cd learning-platformCreate a
.env
file in the root directory by copying the provided
.env.example
file:
cp .env.example .envUpdate the
.env
file with the required values:
# Example configuration
DATABASE_URL="mongodb://admin:admin123@mongodb:27017/learning-platform?authSource=admin&replicaSet=rs0"
NEXTAUTH_URL="http://localhost:3000"
NEXTAUTH_SECRET
="
your-secret-key-at-least-32-chars"
RATE_LIMIT_WINDOW=60000
RATE_LIMIT_MAX=1000Run the following command to start all services:
docker-compose up -dThis will start the following services:
- MongoDB: A MongoDB database with authentication and a single-node replica set.
- Prisma Runner: A one-time job to generate the Prisma client.
- Learning Platform App: The Next.js application running on
http://localhost:3000.
The
mongodb
service is configured to use a single-node replica set. Ensure the replica set is properly initialized by checking the logs:
docker logs learning-platform-mongodbYou should see a message indicating the replica set is active.
Run the database seed script to populate initial data (e.g., roles):
docker exec -it prisma-runner npx prisma db seedThis will create default roles (USER and ADMIN) in the database.
Once all services are running, open your browser and navigate to:
http://localhost:3000
You can now use the Learning Platform.
To stop all services, run:
docker-compose downThis will stop and remove all containers, but data will persist in the mongodb/db volume.
If you encounter issues, check the logs for each service:
docker logs <service-name>Replace <service-name> with one of the following:
learning-platform-mongodb
prisma-runner
learning-platform-app
If you make changes to the application code or dependencies, rebuild the containers:
docker-compose up --build- Database Persistence: MongoDB data is stored in the
mongodb/dbdirectory on your host machine. - Environment Variables: Ensure all required environment variables are set in the
.env
file.
- Feature Flags: Use environment variables to enable/disable specific features.
This project is licensed under the MIT License.
For further assistance, refer to the project documentation or contact the development team.
kalaipuli: here is the right docker compose which includes prisma runner
GitHub Copilot: Here is the corrected
docker-compose.yml
file that includes the
prisma-runner
service and adheres to the modular structure and deployment requirements:
version: '3.8'
services:
prisma-runner:
image: node:18-alpine
container_name: prisma-runner
working_dir: /app
volumes:
- .:/app
command: >
sh -c "npm install && npx prisma generate && npx prisma db push && node prisma/seed.js"
environment:
- DATABASE_URL=mongodb://admin:admin123@mongodb:27017/learning-platform?authSource=admin&replicaSet=rs0
depends_on:
mongodb:
condition: service_healthy
mongodb-rs-fix:
condition: service_completed_successfully
networks:
- app-network
restart: "no"
app:
image: learning-platform:latest
container_name: learning-platform-app
ports:
- "3000:3000"
environment:
- NODE_ENV=production
- DATABASE_URL=mongodb://admin:admin123@mongodb:27017/learning-platform?authSource=admin&replicaSet=rs0
- NEXTAUTH_URL=http://localhost:3000
- NEXTAUTH_SECRET=+YMHyExHLEvI82kalBHH2thIJ6nhkZLpaQMSpjG2ewQ=
- RATE_LIMIT_WINDOW=60000
- RATE_LIMIT_MAX=1000
depends_on:
prisma-runner:
condition: service_completed_successfully
mongodb:
condition: service_healthy
mongodb-rs-fix:
condition: service_completed_successfully
restart: unless-stopped
networks:
- app-network
mongodb:
image: mongo:latest
container_name: learning-platform-mongodb
restart: unless-stopped
environment:
- MONGO_INITDB_ROOT_USERNAME=admin
- MONGO_INITDB_ROOT_PASSWORD=admin123
- MONGO_INITDB_DATABASE=learning-platform
command: >
bash -c "cp /tmp/mongo-keyfile /etc/mongo-keyfile &&
chown mongodb:mongodb /etc/mongo-keyfile &&
chmod 600 /etc/mongo-keyfile &&
exec docker-entrypoint.sh mongod --auth --keyFile /etc/mongo-keyfile --replSet rs0 --bind_ip_all"
ports:
- "27017:27017"
volumes:
- ./mongodb/db:/data/db
- ./mongodb/mongo-keyfile:/tmp/mongo-keyfile:ro
networks:
- app-network
healthcheck:
test: |
mongosh --host mongodb:27017 -u admin -p admin123 --authenticationDatabase admin --eval "try { rs.status() } catch (err) { rs.initiate({ _id: 'rs0', members: [{ _id: 0, host: 'mongodb:27017' }] }) }"
interval: 5s
timeout: 30s
start_period: 5s
retries: 5
mongodb-rs-fix:
image: mongo:latest
container_name: mongodb-rs-fix
depends_on:
mongodb:
condition: service_healthy
command: >
bash -lc "mongosh --host mongodb:27017 -u admin -p admin123 --authenticationDatabase admin --eval \"try { var cfg = rs.conf(); if (cfg.members && cfg.members.length && cfg.members[0].host !== 'mongodb:27017') { cfg.members[0].host = 'mongodb:27017'; rs.reconfig(cfg, {force: true}); print('Replica set reconfigured to mongodb:27017'); } else { print('Replica set host already mongodb:27017 or no members found'); } } catch (e) { print('Reconfig error: ' + e); }\""
restart: "no"
networks:
- app-network
networks:
app-network:
driver: bridge- **
prisma-runner
Service**:
- Uses the
node:18-alpineimage for lightweight execution. - Mounts the current directory (
.) to/appfor accessing the
prisma
folder and
seed.ts
.
- Executes
npm install,npx prisma generate,npx prisma db push, and seeds the database using
node prisma/seed.js
.
- Volumes:
- Updated volume paths for
mongodb
to use relative paths (./mongodb/...) for portability.
-
Health Checks:
- Ensures MongoDB is healthy before running dependent services.
prisma-runner
and mongodb-rs-fix are one-shot jobs, so their restart policy is set to "no".
- Other services (
app,
mongodb
) use unless-stopped.
This configuration ensures that the
prisma-runner
service initializes the database schema and seeds data before the application starts.
kalaipuli: update the readme based on the modified docker compose
GitHub Copilot: Here is the updated
README.md
file reflecting the changes in the
docker-compose.yml
file, including the
prisma-runner
service:
# Learning Platform Deployment Guide
This document provides comprehensive instructions for deploying the Learning Platform using Docker Compose. The platform is a Next.js application with a MongoDB database, Prisma ORM, and NextAuth for authentication.
---
## Prerequisites
Before deploying the application, ensure the following tools are installed on your system:
- **Docker**: [Install Docker](https://docs.docker.com/get-docker/)
- **Docker Compose**: [Install Docker Compose](https://docs.docker.com/compose/install/)
---
## Deployment Instructions
### 1. Clone the Repository
Clone the Learning Platform repository to your local machine:
```bash
git clone https://github.com/your-repo/learning-platform.git
cd learning-platformCreate a .env file in the root directory by copying the provided .env.example file:
cp
.env.example
.envUpdate the .env file with the required values:
# Example configuration
DATABASE_URL="mongodb://admin:admin123@mongodb:27017/learning-platform?authSource=admin&replicaSet=rs0"
NEXTAUTH_URL="http://localhost:3000"
NEXTAUTH_SECRET="your-secret-key-at-least-32-chars"
RATE_LIMIT_WINDOW=60000
RATE_LIMIT_MAX=1000Run the following command to start all services:
docker-compose up -dThis will start the following services:
- MongoDB: A MongoDB database with authentication and a single-node replica set.
- Prisma Runner: A one-time job to generate the Prisma client, push the schema to the database, and seed initial data.
- Learning Platform App: The Next.js application running on
http://localhost:3000.
The mongodb service is configured to use a single-node replica set. Ensure the replica set is properly initialized by checking the logs:
docker logs learning-platform-mongodbYou should see a message indicating the replica set is active.
Once all services are running, open your browser and navigate to:
http://localhost:3000
You can now use the Learning Platform.
To stop all services, run:
docker-compose downThis will stop and remove all containers, but data will persist in the mongodb/db volume.
If you encounter issues, check the logs for each service:
docker logs <service-name>Replace <service-name> with one of the following:
learning-platform-mongodbprisma-runnerlearning-platform-app
If you make changes to the application code or dependencies, rebuild the containers:
docker-compose up --build- Database Persistence: MongoDB data is stored in the
mongodb/dbdirectory on your host machine. - Environment Variables: Ensure all required environment variables are set in the
.envfile. - Prisma Runner: The
prisma-runnerservice is a one-time job that initializes the database schema and seeds data. It will not restart unless explicitly triggered.
This project is licensed under the MIT License.
For further assistance, refer to the project documentation or contact the development team.