Skip to content

Commit 1f1d9f1

Browse files
committed
PEER-254: Dockerize frontend
1 parent 8f28162 commit 1f1d9f1

File tree

10 files changed

+119
-46
lines changed

10 files changed

+119
-46
lines changed

.env.local

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,7 @@ COLLAB_EXPRESS_ENV=compose
2020
COLLAB_EXPRESS_PORT=9003
2121

2222
MATCHING_EXPRESS_ENV=compose
23-
MATCHING_EXPRESS_PORT=9003
23+
MATCHING_EXPRESS_PORT=9004
24+
25+
FRONTEND_ENV=compose
26+
FRONTEND_PORT=3000

docker-compose.yaml

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,76 +4,101 @@ services:
44
# Databases
55

66
user-db:
7-
hostname: "user-db"
7+
hostname: 'user-db'
88
image: postgres:16.4
9-
container_name: "user-db"
9+
container_name: 'user-db'
1010
build:
1111
context: ./backend/user/src/lib/db
1212
env_file:
1313
- ./backend/user/.env.local
1414
volumes:
15-
- "user-db-docker:${USER_PGDATA}"
15+
- 'user-db-docker:${USER_PGDATA}'
1616
restart: unless-stopped
1717
networks:
1818
- user-db-network
1919

2020
collab-db:
21-
hostname: "collab-db"
22-
image: "postgres:16.4"
23-
container_name: "collab-db"
21+
hostname: 'collab-db'
22+
image: 'postgres:16.4'
23+
container_name: 'collab-db'
2424
build:
2525
context: ./backend/collaboration/src/lib/db
2626
env_file:
2727
- ./backend/collaboration/.env.local
2828
volumes:
29-
- "collab-db-docker:/${COLLAB_PGDATA}"
29+
- 'collab-db-docker:/${COLLAB_PGDATA}'
3030
restart: unless-stopped
3131
networks:
3232
- collab-db-network
3333

3434
question-db:
35-
hostname: "question-db"
35+
hostname: 'question-db'
3636
image: postgres:16.4
37-
container_name: "question-db"
37+
container_name: 'question-db'
3838
build:
3939
context: ./backend/question/src/lib/db
4040
env_file:
4141
- ./backend/question/.env.local
4242
volumes:
43-
- "question-db-docker:${QUESTION_PGDATA}"
43+
- 'question-db-docker:${QUESTION_PGDATA}'
4444
restart: unless-stopped
4545
networks:
4646
- question-db-network
4747

4848
# Services
4949

5050
user-service:
51-
image: "user-express"
52-
container_name: "user-express"
51+
image: 'user-express'
52+
container_name: 'user-express'
5353
build:
5454
context: ./backend/user
5555
dockerfile: ./express.Dockerfile
5656
target: build
57-
args:
57+
args:
5858
# For building with the correct env vars
5959
- env=${USER_EXPRESS_ENV}
6060
- port=${USER_EXPRESS_PORT}
6161
ports:
62-
- "9001:${USER_EXPRESS_PORT}"
62+
- '9001:${USER_EXPRESS_PORT}'
6363
env_file:
6464
- ./backend/user/.env.compose
6565
environment:
6666
# Docker Compose Specific
6767
- EXPRESS_DB_HOST=user-db
6868
- EXPRESS_DB_PORT=5432
69-
volumes:
69+
volumes:
7070
- user-service:/data/user-express
7171
depends_on:
7272
- user-db
7373
networks:
7474
- user-db-network
7575
- user-api-network
76-
entrypoint: ["/bin/sh", "entrypoint.sh"]
76+
entrypoint: ['/bin/sh', 'entrypoint.sh']
77+
78+
# Frontend
79+
80+
frontend:
81+
image: 'frontend'
82+
container_name: 'frontend'
83+
build:
84+
context: ./frontend
85+
dockerfile: ./frontend.Dockerfile
86+
args:
87+
- VITE_USER_SERVICE
88+
- VITE_QUESTION_SERVICE
89+
- port=${FRONTEND_PORT}
90+
ports:
91+
- '3000:${FRONTEND_PORT}'
92+
env_file:
93+
- ./frontend/.env.compose
94+
environment:
95+
- FRONTEND_PORT=PORT
96+
depends_on:
97+
- user-service
98+
networks:
99+
- user-api-network
100+
- question-api-network
101+
- collab-api-network
77102

78103
volumes:
79104
user-service:
@@ -91,7 +116,6 @@ volumes:
91116
question-db-docker:
92117
external: true
93118

94-
95119
networks:
96120
# Isolated API Server Networks
97121
user-db-network:

frontend/.env.compose

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
FRONTEND_ENV="compose"
2+
3+
VITE_USER_SERVICE="http://user-service:9001"
4+
VITE_QUESTION_SERVICE="http://question-service:9002"
5+
6+
FRONTEND_PORT=3000

frontend/.env.docker

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

frontend/.env.local

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

frontend/README.md

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,32 @@
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+
-t frontend-local \
9+
--build-arg port=3000 \
10+
-f frontend.Dockerfile .
11+
```
12+
2. Run this command, from the root folder:
613

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
14+
```sh
15+
make db-up
16+
```
17+
18+
3. Run the necessary migrate and seed commands, if you haven't yet.
19+
20+
4. Run this command to expose the container:
21+
```sh
22+
docker run -p 3000:3000 --env-file ./.env.docker frontend-local
23+
```
24+
25+
## Running with Docker-Compose (Main config)
26+
27+
Edit the variables in the `.env.compose` file and run `make up` from the root folder.
28+
29+
Any startup instructions will be run from `entrypoint.sh` instead.
930

1031
## Expanding the ESLint configuration
1132

@@ -22,7 +43,7 @@ export default tseslint.config({
2243
tsconfigRootDir: import.meta.dirname,
2344
},
2445
},
25-
})
46+
});
2647
```
2748

2849
- Replace `tseslint.configs.recommended` to `tseslint.configs.recommendedTypeChecked` or `tseslint.configs.strictTypeChecked`
@@ -31,7 +52,7 @@ export default tseslint.config({
3152

3253
```js
3354
// eslint.config.js
34-
import react from 'eslint-plugin-react'
55+
import react from 'eslint-plugin-react';
3556

3657
export default tseslint.config({
3758
// Set the react version
@@ -46,5 +67,5 @@ export default tseslint.config({
4667
...react.configs.recommended.rules,
4768
...react.configs['jsx-runtime'].rules,
4869
},
49-
})
70+
});
5071
```

frontend/frontend.Dockerfile

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
FROM node:20-alpine AS build
2+
3+
WORKDIR /app
4+
COPY ./package*.json ./
5+
RUN npm install
6+
COPY ./ ./
7+
8+
ARG VITE_USER_SERVICE
9+
ARG VITE_QUESTION_SERVICE
10+
ENV VITE_USER_SERVICE=${VITE_USER_SERVICE}
11+
ENV VITE_QUESTION_SERVICE=${VITE_QUESTION_SERVICE}
12+
13+
RUN npm run build
14+
15+
FROM node:20-alpine AS production
16+
RUN npm install -g serve
17+
WORKDIR /app
18+
COPY --from=build /app/dist ./dist
19+
20+
ARG port
21+
EXPOSE ${port}
22+
23+
CMD ["serve", , "-s", "dist"]
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
FROM node:20-alpine
22

33
WORKDIR /app/
4-
54
COPY ./frontend/package*.json ./
65
RUN npm install
7-
86
COPY ./frontend/ ./
97

10-
EXPOSE 5173
8+
ARG port
9+
EXPOSE ${port}
1110

1211
CMD ["npm", "run", "dev", "--", "--host"]

frontend/react.Dockerfile

Lines changed: 0 additions & 16 deletions
This file was deleted.

frontend/src/services/question-service.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,12 @@ export async function fetchQuestions(pageNum: number = 0): Promise<IGetQuestions
3636
// TODO: Add error handling and notifs
3737
return questionApiClient
3838
.get(QUESTION_SERVICE_ROUTES.GET_QUESTIONS + `?${params}`)
39-
.then((res) => res.data as IGetQuestionsResponse);
39+
.then((res) => res.data as IGetQuestionsResponse)
40+
.catch((err) => {
41+
console.error(err);
42+
return {
43+
questions: [],
44+
totalQuestions: 0,
45+
} as IGetQuestionsResponse;
46+
});
4047
}

0 commit comments

Comments
 (0)