Skip to content

Commit cb25c64

Browse files
authored
Merge pull request #35 from CS3219-AY2425S1/PEER-254-Dockerize-Frontend
PEER-254 Dockerize Frontend with Nginx
2 parents c72adbe + e310362 commit cb25c64

16 files changed

+187
-39
lines changed

.env.local

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,20 @@ COLLAB_EXPRESS_DB_PORT=5434
1010
MATCHING_PGDATA="/data/collab-db"
1111
MATCHING_EXPRESS_DB_PORT=5434
1212

13+
USER_SERVICE_NAME=user-express
1314
USER_EXPRESS_ENV=compose
1415
USER_EXPRESS_PORT=9001
1516

17+
QUESTION_SERVICE_NAME=question-express
1618
QUESTION_EXPRESS_ENV=compose
1719
QUESTION_EXPRESS_PORT=9002
1820

21+
COLLAB_CONTAINER_NAME=collab-express
1922
COLLAB_EXPRESS_ENV=compose
2023
COLLAB_EXPRESS_PORT=9003
2124

2225
MATCHING_EXPRESS_ENV=compose
23-
MATCHING_EXPRESS_PORT=9003
26+
MATCHING_EXPRESS_PORT=9004
27+
28+
FRONTEND_ENV=compose
29+
FRONTEND_PORT=3000

docker-compose.yaml

Lines changed: 50 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,82 +2,81 @@
22

33
services:
44
# Databases
5-
65
user-db:
7-
hostname: "user-db"
6+
hostname: 'user-db'
87
image: postgres:16.4
9-
container_name: "user-db"
8+
container_name: 'user-db'
109
build:
1110
context: ./backend/user/src/lib/db
1211
env_file:
1312
- ./backend/user/.env.local
1413
volumes:
15-
- "user-db-docker:${USER_PGDATA}"
14+
- 'user-db-docker:${USER_PGDATA}'
1615
restart: unless-stopped
1716
networks:
1817
- user-db-network
1918
healthcheck:
20-
test: ["CMD-SHELL", "pg_isready -U peerprep-user-express -d user"]
19+
test: ['CMD-SHELL', 'pg_isready -U peerprep-user-express -d user']
2120
interval: 10s
2221
retries: 5
2322
start_period: 30s
2423
timeout: 10s
2524

2625
collab-db:
27-
hostname: "collab-db"
28-
image: "postgres:16.4"
29-
container_name: "collab-db"
26+
hostname: 'collab-db'
27+
image: 'postgres:16.4'
28+
container_name: 'collab-db'
3029
build:
3130
context: ./backend/collaboration/src/lib/db
3231
env_file:
3332
- ./backend/collaboration/.env.local
3433
volumes:
35-
- "collab-db-docker:/${COLLAB_PGDATA}"
34+
- 'collab-db-docker:/${COLLAB_PGDATA}'
3635
restart: unless-stopped
3736
networks:
3837
- collab-db-network
3938
healthcheck:
40-
test: ["CMD-SHELL", "pg_isready -U peerprep-collab-express -d collab"]
39+
test: ['CMD-SHELL', 'pg_isready -U peerprep-collab-express -d collab']
4140
interval: 10s
4241
retries: 5
4342
start_period: 30s
4443
timeout: 10s
4544

4645
question-db:
47-
hostname: "question-db"
46+
hostname: 'question-db'
4847
image: postgres:16.4
49-
container_name: "question-db"
48+
container_name: 'question-db'
5049
build:
5150
context: ./backend/question/src/lib/db
5251
env_file:
5352
- ./backend/question/.env.local
5453
volumes:
55-
- "question-db-docker:${QUESTION_PGDATA}"
54+
- 'question-db-docker:${QUESTION_PGDATA}'
5655
restart: unless-stopped
5756
networks:
5857
- question-db-network
58+
ports:
59+
- 5433:5432
5960
healthcheck:
60-
test: ["CMD-SHELL", "pg_isready -U peerprep-qn-express -d question"]
61+
test: ['CMD-SHELL', 'pg_isready -U peerprep-qn-express -d question']
6162
interval: 10s
6263
retries: 5
6364
start_period: 30s
6465
timeout: 10s
6566

6667
# Services
67-
6868
user-service:
69-
image: "user-express"
70-
container_name: "user-express"
69+
image: 'user-express'
70+
container_name: '${USER_SERVICE_NAME}'
7171
build:
7272
context: ./backend/user
7373
dockerfile: express.Dockerfile
7474
target: production
75-
args:
75+
args:
7676
# For building with the correct env vars
77-
- env=${USER_EXPRESS_ENV}
7877
- port=${USER_EXPRESS_PORT}
7978
ports:
80-
- "9001:${USER_EXPRESS_PORT}"
79+
- '9001:${USER_EXPRESS_PORT}'
8180
env_file:
8281
- ./backend/user/.env.compose
8382
environment:
@@ -93,11 +92,39 @@ services:
9392
- user-api-network
9493
healthcheck:
9594
test: wget --no-verbose --tries=1 --spider http://localhost:9001/health || exit 1
96-
interval: 30s
97-
timeout: 10s
98-
retries: 5
95+
interval: 30s
96+
timeout: 10s
97+
retries: 5
9998
start_period: 5s
10099

100+
# Frontend
101+
102+
frontend:
103+
container_name: 'frontend'
104+
build:
105+
context: ./frontend
106+
dockerfile: ./frontend.Dockerfile
107+
target: production
108+
args:
109+
- FRONTEND_PORT=${FRONTEND_PORT}
110+
ports:
111+
- '3000:${FRONTEND_PORT}'
112+
env_file:
113+
- ./frontend/.env.compose
114+
environment:
115+
- VITE_USER_SERVICE=http://${USER_SERVICE_NAME}:${USER_EXPRESS_PORT}
116+
##to remove localhost once integrating question
117+
- VITE_QUESTION_SERVICE=http://host.docker.internal:${QUESTION_EXPRESS_PORT}
118+
- FRONTEND_PORT=${FRONTEND_PORT}
119+
depends_on:
120+
user-service:
121+
condition: service_healthy
122+
restart: true
123+
networks:
124+
- user-api-network
125+
- question-api-network
126+
- collab-api-network
127+
101128
volumes:
102129
# Persistent Volumes for Databases
103130
user-db-docker:
@@ -107,7 +134,6 @@ volumes:
107134
question-db-docker:
108135
external: true
109136

110-
111137
networks:
112138
# Isolated API Server Networks
113139
user-db-network:

frontend/.dockerignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
node_modules
2+
dist
3+
frontend.*Dockerfile
4+
.dockerignore

frontend/.env.compose

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
FRONTEND_ENV=compose

frontend/.env.docker

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
FRONTEND_ENV=local
2+
3+
VITE_USER_SERVICE=http://host.docker.internal:9001
4+
VITE_QUESTION_SERVICE=http://host.docker.internal:9002
5+
FRONTEND_PORT=3000

frontend/.env.local

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1-
VITE_USER_SERVICE="http://localhost:9001"
2-
VITE_QUESTION_SERVICE="http://localhost:9002"
1+
FRONTEND_ENV=local
2+
3+
VITE_USER_SERVICE=http://localhost:9001
4+
VITE_QUESTION_SERVICE=http://localhost:9002

frontend/README.md

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,33 @@
1-
# React + TypeScript + Vite
1+
# Frontend
22

3-
This template provides a minimal setup to get React working in Vite with HMR and some ESLint rules.
3+
## Running with Docker (Standalone)
44

5-
Currently, two official plugins are available:
5+
1. Run this command to build:
6+
```sh
7+
docker build \
8+
--build-arg VITE_USER_SERVICE=http://host.docker.internal:9001 \
9+
--build-arg VITE_QUESTION_SERVICE=http://host.docker.internal:9002 \
10+
--build-arg FRONTEND_PORT=3000 \
11+
-t frontend-app -f frontend.Dockerfile .
12+
```
13+
2. Run this command, from the root folder:
614

7-
- [@vitejs/plugin-react](https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react/README.md) uses [Babel](https://babeljs.io/) for Fast Refresh
8-
- [@vitejs/plugin-react-swc](https://github.com/vitejs/vite-plugin-react-swc) uses [SWC](https://swc.rs/) for Fast Refresh
15+
```sh
16+
make db-up
17+
```
18+
19+
3. Run the necessary migrate and seed commands, if you haven't yet.
20+
21+
4. Run this command to expose the container:
22+
```sh
23+
docker run -p 3000:3000 --env-file ./.env.docker frontend-app
24+
```
25+
26+
## Running with Docker-Compose (Main config)
27+
28+
Edit the variables in the `.env.compose` file and run `make up` from the root folder.
29+
30+
Any startup instructions will be run from `entrypoint.sh` instead.
931

1032
## Expanding the ESLint configuration
1133

@@ -22,7 +44,7 @@ export default tseslint.config({
2244
tsconfigRootDir: import.meta.dirname,
2345
},
2446
},
25-
})
47+
});
2648
```
2749

2850
- Replace `tseslint.configs.recommended` to `tseslint.configs.recommendedTypeChecked` or `tseslint.configs.strictTypeChecked`
@@ -31,7 +53,7 @@ export default tseslint.config({
3153

3254
```js
3355
// eslint.config.js
34-
import react from 'eslint-plugin-react'
56+
import react from 'eslint-plugin-react';
3557

3658
export default tseslint.config({
3759
// Set the react version
@@ -46,5 +68,5 @@ export default tseslint.config({
4668
...react.configs.recommended.rules,
4769
...react.configs['jsx-runtime'].rules,
4870
},
49-
})
71+
});
5072
```

frontend/entrypoint.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/sh
2+
3+
envsubst '${FRONTEND_PORT} ${VITE_USER_SERVICE} ${VITE_QUESTION_SERVICE}' < /etc/nginx/nginx.conf.template > /etc/nginx/conf.d/default.conf
4+
5+
nginx -g 'daemon off;'

frontend/frontend.Dockerfile

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
FROM node:20-alpine AS build
2+
3+
WORKDIR /app
4+
COPY ./package*.json ./
5+
RUN npm install
6+
COPY ./ ./
7+
8+
RUN npm run build
9+
10+
FROM nginx:stable-alpine AS production
11+
12+
COPY --from=build /app/build /usr/share/nginx/html
13+
14+
COPY ./nginx.conf.template /etc/nginx/nginx.conf.template
15+
COPY entrypoint.sh /usr/local/bin/
16+
RUN chmod +x /usr/local/bin/entrypoint.sh
17+
18+
ARG FRONTEND_PORT
19+
EXPOSE ${FRONTEND_PORT}
20+
21+
ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]

frontend/frontend.dev.Dockerfile

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
FROM node:20-alpine
2+
3+
WORKDIR /app/
4+
COPY ./frontend/package*.json ./
5+
RUN npm install
6+
COPY ./frontend/ ./
7+
8+
ARG port
9+
EXPOSE ${port}
10+
11+
CMD ["npm", "run", "dev", "--", "--host"]

0 commit comments

Comments
 (0)