Skip to content

Commit 73ecfef

Browse files
authored
Merge pull request #34 from CS3219-AY2425S1/staging
Milestone D3
2 parents e50b35a + 65f0d57 commit 73ecfef

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

92 files changed

+6592
-2800
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
apps/question-service/.next/
2+
apps/question-service/next-env.d.ts
3+
apps/question-service/node_modules/
4+
services/question-service/cs3219-g24-firebase-adminsdk-9cm7h-b1675603ab.json
5+

apps/README.md

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# PeerPrep Docker Compose Guide
2+
3+
This project uses Docker Compose to manage multiple services such as a frontend, backend, and a database. The configuration is defined in the `docker-compose.yml` file, and environment variables can be stored in environment files for different environments (e.g., development, production).
4+
5+
## Prerequisites
6+
7+
Before you begin, ensure you have the following installed on your machine:
8+
9+
- [Docker](https://www.docker.com/get-started)
10+
- [Docker Compose](https://docs.docker.com/compose/install/)
11+
12+
## Project Structure
13+
14+
In the `./apps` directory:
15+
16+
```plaintext
17+
.
18+
├── docker-compose.yml # Docker Compose configuration
19+
├── .env # Global environment variables (optional)
20+
├── frontend
21+
│ ├── Dockerfile # Dockerfile for frontend
22+
│ └── ... (other frontend files)
23+
├── question-service
24+
│ ├── Dockerfile # Dockerfile for question-service
25+
│ └── ... (other question-service files)
26+
├── user-service
27+
│ ├── Dockerfile # Dockerfile for user-service
28+
│ └── ... (other user-service files)
29+
└── README.md # Project documentation (for docker compose)
30+
```
31+
32+
## Docker Compose Setup
33+
34+
By using multiple Dockerfiles in Docker Compose, we can manage complex multi-container applications where each service has its own environment and build process.
35+
36+
1. Build and Start the Application
37+
38+
To build and run both the frontend and backend services, you can change your directory to the `./apps` directory and run:
39+
40+
```bash
41+
docker-compose up --build
42+
```
43+
44+
This will:
45+
46+
- Build the Docker images for all services using the specified Dockerfiles
47+
- Start the containers and map the defined ports
48+
49+
2. Access the Application
50+
51+
Once running, you can access:
52+
53+
- The **frontend** at http://localhost:3000
54+
- The **user service** at http://localhost:3001
55+
- The **question service** at http://localhost:8080
56+
57+
3. Stopping Services
58+
59+
To stop the running services, run:
60+
61+
```bash
62+
docker-compose down
63+
```
64+
65+
This command will stop and remove the containers, networks, and volumes created by docker-compose up.
66+
67+
## Troubleshooting
68+
69+
**Common Issues**
70+
71+
- Port Conflicts: If you encounter port conflicts, ensure the host ports specified in docker-compose.yml (e.g., 3000:3000) are not in use by other applications.
72+
- Environment Variables Not Loaded: Ensure the `.env` files are in the correct directories as found in the `docker-compose.yml` file.
73+
74+
**Logs**
75+
76+
You can view the logs for each service using the following command:
77+
78+
```bash
79+
docker-compose logs
80+
```
81+
82+
**Useful Commands**
83+
84+
Rebuild a specific service:
85+
86+
```bash
87+
docker-compose build <service_name>
88+
```
89+
90+
Start services in detached mode (run in the background):
91+
92+
```bash
93+
docker-compose up -d
94+
```
95+
96+
Remove all containers, networks, and volumes created by Docker Compose:
97+
98+
```bash
99+
docker-compose down --volumes
100+
```

apps/docker-compose.yml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
services:
2+
frontend:
3+
build:
4+
context: ./frontend
5+
dockerfile: Dockerfile
6+
ports:
7+
- 3000:3000
8+
networks:
9+
- apps_network
10+
env_file:
11+
- ./frontend/.env
12+
volumes:
13+
- ./frontend:/frontend
14+
15+
user-service:
16+
build:
17+
context: ./user-service
18+
dockerfile: Dockerfile
19+
ports:
20+
- 3001:3001
21+
networks:
22+
- apps_network
23+
env_file:
24+
- ./user-service/.env
25+
volumes:
26+
- ./user-service:/user-service
27+
28+
question-service:
29+
build:
30+
context: ./question-service
31+
dockerfile: Dockerfile
32+
ports:
33+
- 8080:8080
34+
env_file:
35+
- ./question-service/.env
36+
networks:
37+
- apps_network
38+
volumes:
39+
- ./question-service:/question-service
40+
41+
networks:
42+
apps_network:

apps/frontend/.dockerignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
node_modules
2+
.dockerignore
3+
.git
4+
.gitignore
5+
.next
6+
out
7+
public
8+
README.md

apps/frontend/.env.example

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Replace with the corresponding url, credentials and endpoint
2+
NEXT_PUBLIC_QUESTION_SERVICE_URL="http://localhost:8080/"
3+
NEXT_PUBLIC_USER_SERVICE_URL="http://localhost:3001/"
File renamed without changes.

apps/frontend/.gitignore

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2+
3+
# dependencies
4+
/node_modules
5+
/.pnp
6+
.pnp.js
7+
.yarn/install-state.gz
8+
9+
# testing
10+
/coverage
11+
12+
# next.js
13+
/.next/
14+
/out/
15+
16+
# production
17+
/build
18+
19+
# misc
20+
.DS_Store
21+
*.pem
22+
23+
# debug
24+
npm-debug.log*
25+
yarn-debug.log*
26+
yarn-error.log*
27+
28+
# local env files
29+
.env*.local
30+
.env
31+
32+
# vercel
33+
.vercel
34+
35+
# typescript
36+
*.tsbuildinfo
37+
next-env.d.ts

apps/frontend/Dockerfile

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
FROM node:18-alpine AS base
2+
3+
# Install dependencies only when needed
4+
FROM base AS deps
5+
# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
6+
RUN apk add --no-cache libc6-compat
7+
WORKDIR /app
8+
9+
# Install dependencies based on the preferred package manager
10+
COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./
11+
RUN \
12+
if [ -f pnpm-lock.yaml ]; then corepack enable pnpm && pnpm i --frozen-lockfile; \
13+
else echo "Lockfile not found." && exit 1; \
14+
fi
15+
16+
# Rebuild the source code only when needed
17+
FROM base AS builder
18+
WORKDIR /app
19+
COPY --from=deps /app/node_modules ./node_modules
20+
COPY . .
21+
22+
# Next.js collects completely anonymous telemetry data about general usage.
23+
# Learn more here: https://nextjs.org/telemetry
24+
# Uncomment the following line in case you want to disable telemetry during the build.
25+
# ENV NEXT_TELEMETRY_DISABLED=1
26+
27+
RUN \
28+
if [ -f pnpm-lock.yaml ]; then corepack enable pnpm && pnpm run build; \
29+
else echo "Lockfile not found." && exit 1; \
30+
fi
31+
32+
# Production image, copy all the files and run next
33+
FROM base AS runner
34+
WORKDIR /app
35+
36+
ENV NODE_ENV=production
37+
# Uncomment the following line in case you want to disable telemetry during runtime.
38+
# ENV NEXT_TELEMETRY_DISABLED=1
39+
40+
RUN addgroup --system --gid 1001 nodejs
41+
RUN adduser --system --uid 1001 nextjs
42+
43+
# Include this when we want to include images in the future
44+
# COPY --from=builder /app/public ./public
45+
46+
# Set the correct permission for prerender cache
47+
RUN mkdir .next
48+
RUN chown nextjs:nodejs .next
49+
50+
# Automatically leverage output traces to reduce image size
51+
# https://nextjs.org/docs/advanced-features/output-file-tracing
52+
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
53+
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
54+
55+
USER nextjs
56+
57+
EXPOSE 3000
58+
59+
ENV PORT=3000
60+
61+
# server.js is created by next build from the standalone output
62+
# https://nextjs.org/docs/pages/api-reference/next-config-js/output
63+
ENV HOSTNAME="0.0.0.0"
64+
CMD ["node", "server.js"]

apps/frontend/README.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
This is the frontend for the question service.
2+
3+
## Tech Stack
4+
5+
- Next.js
6+
- TypeScript
7+
- Ant Design
8+
- SCSS
9+
10+
## Getting Started
11+
12+
First, install the dependencies:
13+
14+
```bash
15+
npm install -g pnpm
16+
17+
pnpm install --frozen-lockfile
18+
19+
# if pnpm install --frozen-lockfile fails, try running
20+
pnpm install
21+
```
22+
23+
Then, follow the `.env.example` file and create a `.env` file in the current directory. Replace the necessary values within.
24+
25+
```bash
26+
NEXT_PUBLIC_QUESTION_SERVICE_URL="http://localhost:8080"
27+
NEXT_PUBLIC_USER_SERVICE_URL="http://localhost:3001/"
28+
```
29+
30+
First, run the development server:
31+
32+
```bash
33+
pnpm dev
34+
```
35+
36+
Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.
37+
38+
You can start editing the page by modifying `app/page.tsx`. The page auto-updates as you edit the file.
39+
40+
## Build Dockerfile
41+
42+
```sh
43+
# Navigate to the frontend app directory
44+
cd apps/frontend
45+
46+
# Build dockerfile (Ensure that your docker daemon is running beforehand)
47+
docker build -t frontend -f Dockerfile .
48+
```
49+
50+
Run the backend server locally and visit http://localhost:3000/ to see the frontend application working
51+
52+
## Running Docker Image
53+
54+
```sh
55+
# Run the docker image, the -d tag is to run it detached
56+
docker run -p 3000:3000 --env-file .env -d frontend
57+
58+
# To see the running container
59+
docker ps
60+
61+
# To stop the container, copy the container id from the previous command
62+
docker stop <container_id>
63+
```
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
/** @type {import('next').NextConfig} */
2-
const nextConfig = {};
2+
const nextConfig = {
3+
output: "standalone",
4+
};
35

46
export default nextConfig;

0 commit comments

Comments
 (0)