Skip to content

Commit

Permalink
Merge pull request #34 from CS3219-AY2425S1/anun/docker-question
Browse files Browse the repository at this point in the history
PEER-252 Dockerize question service
  • Loading branch information
anunayajoshi authored Oct 6, 2024
2 parents cb25c64 + 7b32a74 commit 4824c96
Show file tree
Hide file tree
Showing 10 changed files with 135 additions and 22 deletions.
12 changes: 12 additions & 0 deletions backend/question/.env.compose
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
EXPRESS_ENV="compose"
PEERPREP_UI_HOST="http://frontend:3000"

EXPRESS_PORT=9002
EXPRESS_DB_HOST="qn-db"
EXPRESS_DB_PORT=5433
POSTGRES_DB="question"
POSTGRES_USER="peerprep-qn-express"
POSTGRES_PASSWORD="Xk8qEcEI2sizjfEn/lF6mLqiyBECjIHY3q6sdXf9poQ="
PGDATA="/data/qn-db"

EXPRESS_JWT_SECRET_KEY="jd+9qlXA0a3YsmVf2KJgyiJ3SprIR318IAwhRXck4Y8="
12 changes: 12 additions & 0 deletions backend/question/.env.docker
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
EXPRESS_ENV=local
PEERPREP_UI_HOST=http://localhost:5173

EXPRESS_PORT=9002
EXPRESS_DB_HOST=host.docker.internal
EXPRESS_DB_PORT=5433
POSTGRES_DB=question
POSTGRES_USER=peerprep-qn-express
POSTGRES_PASSWORD=Xk8qEcEI2sizjfEn/lF6mLqiyBECjIHY3q6sdXf9poQ=
PGDATA=/data/qn-db

EXPRESS_JWT_SECRET_KEY=jd+9qlXA0a3YsmVf2KJgyiJ3SprIR318IAwhRXck4Y8=
34 changes: 34 additions & 0 deletions backend/question/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,40 @@
This directory contains the code for the Template
Service.


# Questions Service

## Running with Docker (Standalone)

1. Run this command to build:
```sh
docker build \
-t question-express-local \
--build-arg port=9002 \
-f express.Dockerfile .
```
2. Run this command, from the root folder:
```sh
make db-up
```

3. Run the necessary migrate and seed commands, if you haven't yet.
4. Run this command to expose the container:
```sh
docker run -p 9002:9002 --env-file ./.env.docker question-express-local
```
## Running with Docker-Compose (Main config)
Edit the variables in the `.env.compose` file and run `make up` from the root folder.
Any startup instructions will be run from `entrypoint.sh` instead.
## Database
We use:
Expand Down
4 changes: 2 additions & 2 deletions backend/question/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ services:
target: build
args:
# For building with the correct env vars
- env=${EXPRESS_ENV}
- port=${EXPRESS_PORT}
ports:
- "9002:8001"
- "9001:${EXPRESS_PORT}"
command: node dist/index.js
env_file:
- ./.env.local
Expand Down
13 changes: 13 additions & 0 deletions backend/question/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/sh

# Drizzle will handle its own logic to remove conflicts
npm run db:prod:migrate

# Checks admin table and will not seed if data exists
npm run db:prod:seed

rm -rf drizzle src tsconfig.json

npm uninstall tsx drizzle-kit

npm run start
19 changes: 14 additions & 5 deletions backend/question/express.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,25 @@ FROM node:lts-alpine AS build
WORKDIR /data/question-express
COPY package*.json ./
RUN npm install
ARG env
COPY . .
RUN npm run build

FROM node:lts-alpine AS production
WORKDIR /data/question-express
COPY --from=build /data/question-express/package*.json ./
RUN npm ci --omit=dev
COPY --from=build --chown=node:node /data/question-express/dist ./dist

ARG env
COPY ".env.${env}" .
EXPOSE 8001
CMD [ "npm", "run", "start" ]
RUN npm ci --omit=dev

# For migration
RUN npm install tsx drizzle-kit
COPY drizzle ./drizzle
COPY src/lib/db/ ./src/lib/db
COPY src/config.ts ./src
COPY tsconfig.json .
COPY entrypoint.sh .

ARG port
EXPOSE ${port}
ENTRYPOINT [ "/bin/sh", "entrypoint.sh" ]
12 changes: 8 additions & 4 deletions backend/question/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
"main": "dist/index.js",
"scripts": {
"dev": "env-cmd -f .env.local nodemon src/index.ts | pino-pretty",
"build": "env-cmd -f .env.local tsc && tsc-alias",
"start": "env-cmd -f .env.local node dist/index.js",
"build:prod": "env-cmd -f .env.prod tsc && tsc-alias",
"start:prod": "env-cmd -f .env.local node dist/index.js",
"build": "tsc && tsc-alias",
"start": "node dist/index.js",
"build:local": "env-cmd -f .env.local tsc && tsc-alias",
"start:local": "env-cmd -f .env.local node dist/index.js",
"db:generate": "env-cmd -f .env.local drizzle-kit generate",
"db:migrate": "env-cmd -f .env.local tsx ./src/lib/db/migrate.ts",
"db:seed": "env-cmd -f .env.local tsx ./src/lib/db/seed.ts",
"db:prod:migrate": "tsx ./src/lib/db/migrate.ts",
"db:prod:seed": "tsx ./src/lib/db/seed.ts",
"db:inspect": "env-cmd -f .env.local drizzle-kit studio",
"fmt": "prettier --config .prettierrc src --write",
"test": "echo \"Error: no test specified\" && exit 1"
Expand All @@ -21,6 +23,7 @@
"description": "",
"dependencies": {
"cors": "^2.8.5",
"dotenv": "^16.4.5",
"drizzle-orm": "^0.33.0",
"env-cmd": "^10.1.0",
"express": "^4.21.0",
Expand All @@ -33,6 +36,7 @@
"devDependencies": {
"@swc/core": "^1.7.26",
"@swc/helpers": "^0.5.13",
"@types/cors": "^2.8.17",
"@types/express": "^4.17.21",
"@types/node": "^22.5.5",
"drizzle-kit": "^0.24.2",
Expand Down
3 changes: 2 additions & 1 deletion backend/question/src/lib/db/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const seedQuestions = async () => {
const seedRecords = await trx.select().from(adminTable).where(eq(adminTable.action, 'SEED'));
if (seedRecords && seedRecords.length > 0) {
console.info(
`[Users]: Seeded already at: ${(seedRecords[seedRecords.length - 1].createdAt ?? new Date()).toLocaleString()}`
`[Questions]: Seeded already at: ${(seedRecords[seedRecords.length - 1].createdAt ?? new Date()).toLocaleString()}`
);
return;
}
Expand All @@ -32,6 +32,7 @@ const seedQuestions = async () => {
.values({ ...question, id: undefined }) // Let DB set ID
.onConflictDoNothing();
}
await trx.insert(adminTable).values({ action: 'SEED' });
});
} catch (error) {
console.log('[Questions]: Error seeding question data', error);
Expand Down
44 changes: 36 additions & 8 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ services:
# Databases
user-db:
hostname: 'user-db'
image: postgres:16.4
image: 'postgres:16.4'
container_name: 'user-db'
build:
context: ./backend/user/src/lib/db
Expand Down Expand Up @@ -55,8 +55,6 @@ services:
restart: unless-stopped
networks:
- question-db-network
ports:
- 5433:5432
healthcheck:
test: ['CMD-SHELL', 'pg_isready -U peerprep-qn-express -d question']
interval: 10s
Expand All @@ -75,8 +73,6 @@ services:
args:
# For building with the correct env vars
- port=${USER_EXPRESS_PORT}
ports:
- '9001:${USER_EXPRESS_PORT}'
env_file:
- ./backend/user/.env.compose
environment:
Expand All @@ -91,12 +87,42 @@ services:
- user-db-network
- user-api-network
healthcheck:
test: wget --no-verbose --tries=1 --spider http://localhost:9001/health || exit 1
test: wget --no-verbose --tries=1 --spider http://localhost:${USER_EXPRESS_PORT}/health || exit 1
interval: 30s
timeout: 10s
retries: 5
start_period: 5s

question-service:
image: "question-express"
container_name: '${QUESTION_SERVICE_NAME}'
build:
context: ./backend/question
dockerfile: express.Dockerfile
target: production
args:
# For building with the correct env vars
- port=${QUESTION_EXPRESS_PORT}
env_file:
- ./backend/question/.env.compose
environment:
# Docker Compose Specific for Service Discovery
- EXPRESS_DB_HOST=question-db
- EXPRESS_DB_PORT=5432
depends_on:
question-db:
condition: service_healthy
restart: true
networks:
- question-db-network
- question-api-network
healthcheck:
test: wget --no-verbose --tries=1 --spider http://localhost:${QUESTION_EXPRESS_PORT}/health || exit 1
interval: 30s
timeout: 10s
retries: 5
start_period: 5s

# Frontend

frontend:
Expand All @@ -113,13 +139,15 @@ services:
- ./frontend/.env.compose
environment:
- VITE_USER_SERVICE=http://${USER_SERVICE_NAME}:${USER_EXPRESS_PORT}
##to remove localhost once integrating question
- VITE_QUESTION_SERVICE=http://host.docker.internal:${QUESTION_EXPRESS_PORT}
- VITE_QUESTION_SERVICE=http://${QUESTION_SERVICE_NAME}:${QUESTION_EXPRESS_PORT}
- FRONTEND_PORT=${FRONTEND_PORT}
depends_on:
user-service:
condition: service_healthy
restart: true
question-service:
condition: service_healthy
restart: true
networks:
- user-api-network
- question-api-network
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4824c96

Please sign in to comment.