Skip to content

kalaipuli/learning-platform

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

129 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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:


Deployment Instructions

1. Clone the Repository

Clone the Learning Platform repository to your local machine:

git clone https://github.com/your-repo/learning-platform.git
cd learning-platform

2. Configure Environment Variables

Create a

.env

file in the root directory by copying the provided

.env.example

file:

cp .env.example .env

Update 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=1000

3. Start the Application

Run the following command to start all services:

docker-compose up -d

This 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.

4. Verify MongoDB Replica Set

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-mongodb

You should see a message indicating the replica set is active.


5. Seed the Database

Run the database seed script to populate initial data (e.g., roles):

docker exec -it prisma-runner npx prisma db seed

This will create default roles (USER and ADMIN) in the database.


6. Access the Application

Once all services are running, open your browser and navigate to:

http://localhost:3000

You can now use the Learning Platform.


Stopping the Application

To stop all services, run:

docker-compose down

This will stop and remove all containers, but data will persist in the mongodb/db volume.


Troubleshooting

Check Service Logs

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

Rebuild Containers

If you make changes to the application code or dependencies, rebuild the containers:

docker-compose up --build

Additional Notes

  • Database Persistence: MongoDB data is stored in the mongodb/db directory 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.

License

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

Key Changes:

  1. **

prisma-runner

Service**:

  • Uses the node:18-alpine image for lightweight execution.
  • Mounts the current directory (.) to /app for 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

.

  1. Volumes:
    • Updated volume paths for

mongodb

to use relative paths (./mongodb/...) for portability.

  1. Health Checks:

    • Ensures MongoDB is healthy before running dependent services.
  2. Restart Policies:

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-platform

2. Configure Environment Variables

Create a .env file in the root directory by copying the provided .env.example file:

cp 

.env.example

 .env

Update 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=1000

3. Start the Application

Run the following command to start all services:

docker-compose up -d

This 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.

4. Verify MongoDB Replica Set

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-mongodb

You should see a message indicating the replica set is active.


5. Access the Application

Once all services are running, open your browser and navigate to:

http://localhost:3000

You can now use the Learning Platform.


Stopping the Application

To stop all services, run:

docker-compose down

This will stop and remove all containers, but data will persist in the mongodb/db volume.


Troubleshooting

Check Service Logs

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

Rebuild Containers

If you make changes to the application code or dependencies, rebuild the containers:

docker-compose up --build

Additional Notes

  • Database Persistence: MongoDB data is stored in the mongodb/db directory on your host machine.
  • Environment Variables: Ensure all required environment variables are set in the .env file.
  • Prisma Runner: The prisma-runner service is a one-time job that initializes the database schema and seeds data. It will not restart unless explicitly triggered.

License

This project is licensed under the MIT License.

For further assistance, refer to the project documentation or contact the development team.

About

No description, website, or topics provided.

Resources

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages