fix(tools): consolidate docker compose setup (#62525)

This commit is contained in:
Mrugesh Mohapatra
2025-10-07 10:49:41 +05:30
committed by GitHub
parent f002f1b835
commit 3cbe2ab8b1
11 changed files with 43 additions and 107 deletions
-46
View File
@@ -1,46 +0,0 @@
# Working on the new api
## Connecting to local database
The api uses the ORM Prisma and it needs the MongoDB instance to be a replica set.
### Atlas
If you use MongoDB Atlas, the set is managed for you.
### Local
The simplest way to run a replica set locally is to use the docker-compose file
in /tools.
```bash
cd tools
docker compose up -d
```
The new db will be empty, so you can run the seed script to populate it.
```bash
cd ../.. # back to the root of the repo
pnpm seed
```
### Troubleshooting
If you have any issues connecting to the database (e.g. MongoServerError: not primary), try removing the volume and recreating the containers.
```bash
cd tools
docker compose down -v
docker compose up -d
```
## Login in development/testing
During development and testing, the api exposes the endpoint GET auth/dev-callback. Calling this will log you in as the user with the email `foo@bar.com` by setting the session cookie for that user.
## Generating Exams
```bash
pnpm run exam-env:generate <ENV_EXAM_ID> <NUMBER_OF_EXAMS_TO_GENERATE>
```
+2 -2
View File
@@ -1,7 +1,7 @@
import nodemailer, { Transporter } from 'nodemailer';
import { MailProvider, SendEmailArgs } from '../mailer.js';
import { MAILHOG_HOST } from '../../utils/env.js';
import { MAILPIT_HOST } from '../../utils/env.js';
/**
* NodemailerProvider is a wrapper around nodemailer that provides a clean
@@ -16,7 +16,7 @@ export class NodemailerProvider implements MailProvider {
*/
constructor() {
this.transporter = nodemailer.createTransport({
host: MAILHOG_HOST,
host: MAILPIT_HOST,
secure: false,
port: 1025,
auth: {
+6 -1
View File
@@ -159,7 +159,12 @@ if (process.env.FREECODECAMP_NODE_ENV !== 'development') {
export const HOME_LOCATION = process.env.HOME_LOCATION;
// Mailpit is used in development and test environments, hence the localhost
// default.
export const MAILHOG_HOST = process.env.MAILHOG_HOST ?? 'localhost';
// TODO: Remove MAILHOG_HOST in a few months
// We renamed MailHog to MailPit, but kept the same port and API
// This is to keep backward compatibility with existing setups
// that might still use MAILHOG_HOST environment variable
export const MAILPIT_HOST =
process.env.MAILPIT_HOST ?? process.env.MAILHOG_HOST ?? 'localhost';
export const MONGOHQ_URL =
process.env.NODE_ENV === 'test'
? createTestConnectionURL(
-34
View File
@@ -1,34 +0,0 @@
services:
db:
image: mongo
container_name: mongodb
command: mongod --replSet rs0
restart: unless-stopped
ports:
- 27017:27017
volumes:
- db-data:/data/db
healthcheck:
test: ['CMD', 'mongosh', '--eval', "db.adminCommand('ping')"]
interval: 2s
retries: 5
setup:
image: mongo
depends_on:
db:
condition: service_healthy
restart: on-failure
# This will try to initiate the replica set, until it succeeds twice (i.e. until the replica set is already initialized)
command: >
mongosh --host db:27017 --eval '
try {
rs.initiate();
} catch (err) {
if(err.codeName !== "AlreadyInitialized") throw err;
}
'
volumes:
db-data:
driver: local