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
+1 -1
View File
@@ -160,7 +160,7 @@ jobs:
- name: Start apps
run: |
docker compose up -d
docker compose -f docker/docker-compose.yml -f docker/docker-compose.e2e.yml up -d
pnpm run serve:client-ci &
sleep 10
+1 -1
View File
@@ -152,7 +152,7 @@ jobs:
- name: Start apps
run: |
docker compose up -d
docker compose -f docker/docker-compose.yml -f docker/docker-compose.e2e.yml up -d
pnpm run serve:client-ci &
sleep 10
+1 -2
View File
@@ -42,8 +42,7 @@ tasks:
- name: db
# starting mongod in background, so it doesn't block prebuilds
before: >
cd api/tools &&
docker compose up -d
docker compose -f docker/docker-compose.yml up -d
- name: server
before: export COOKIE_DOMAIN=.gitpod.io && export HOME_LOCATION=$(gp url 8000) && export API_LOCATION=$(gp url 3000)
-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(
-39
View File
@@ -1,39 +0,0 @@
services:
mongo:
image: mongo
ports:
- '27017:27017'
command: mongod --replSet rs0
setup:
image: mongo
depends_on:
- mongo
restart: on-failure
entrypoint: [
'bash',
'-c',
# This will try to initiate the replica set, until it succeeds twice (i.e. until the replica set is already initialized)
'mongosh --host mongo:27017 --eval ''try {rs.initiate();} catch (err) { if(err.codeName !== "AlreadyInitialized") throw err };'''
]
mailpit:
restart: unless-stopped
image: axllent/mailpit
ports:
- '1025:1025'
- '8025:8025'
api:
restart: unless-stopped
depends_on:
- mongo
- mailpit
image: fcc-api
env_file:
- .env
environment:
# The api cannot connect to mongodb or mailpit via localhost from inside the
# container, so we have to override these variables.
- MONGOHQ_URL=mongodb://mongo:27017/freecodecamp?directConnection=true
- MAILHOG_HOST=mailpit
- HOST=0.0.0.0
ports:
- '3000:3000'
-14
View File
@@ -1,14 +0,0 @@
services:
api:
restart: unless-stopped
image: fcc-api
env_file:
- ../../.env
ports:
- '3000:3000'
logging:
driver: 'local'
options:
max-size: '10m'
max-file: '3'
compress: 'true'
+19
View File
@@ -0,0 +1,19 @@
# Docker Compose override for E2E testing
# Usage: docker compose -f docker/docker-compose.yml -f docker/docker-compose.e2e.yml up -d
services:
api:
restart: unless-stopped
depends_on:
- db
- mailpit
image: fcc-api
env_file:
- ../.env
environment:
# The api cannot connect to mongodb or mailpit via localhost from inside the
# container, so we have to override these variables.
- MONGOHQ_URL=mongodb://db:27017/freecodecamp?directConnection=true
- MAILHOG_HOST=mailpit
- HOST=0.0.0.0
ports:
- '3000:3000'
@@ -29,6 +29,13 @@ services:
}
'
mailpit:
restart: unless-stopped
image: axllent/mailpit
ports:
- '1025:1025'
- '8025:8025'
volumes:
db-data:
driver: local
+6 -1
View File
@@ -11,7 +11,12 @@ type AllEmails = {
count: number;
};
const 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
const host =
process.env.MAILPIT_HOST || process.env.MAILHOG_HOST || 'localhost';
export const getAllEmails = async (): Promise<AllEmails> => {
const res = await fetch(`http://${host}:8025/api/v1/messages`);