Skip to content

Commit 52d5570

Browse files
committed
PEER-255 Add docker-compose config for user-db and user-service
Signed-off-by: SeeuSim <[email protected]>
1 parent 7431eab commit 52d5570

File tree

10 files changed

+168
-137
lines changed

10 files changed

+168
-137
lines changed

.env.local

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,6 @@ QUESTION_EXPRESS_DB_PORT=5433
66

77
COLLAB_PGDATA="/data/collab-db"
88
COLLAB_EXPRESS_DB_PORT=5434
9+
10+
USER_EXPRESS_ENV=compose
11+
USER_EXPRESS_PORT=9001

Makefile

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,14 @@ migrate-seed:
77

88
db-up:
99
./scripts/ensure-volume.sh
10-
docker compose --env-file .env.local up -d
10+
docker compose --env-file .env.local -f docker-compose.local.yaml up -d
1111

1212
db-down:
13-
docker compose --env-file .env.local down
13+
docker compose --env-file .env.local -f docker-compose.local.yaml down
14+
15+
up:
16+
./scripts/ensure-volume.sh
17+
docker compose --env-file .env.local up -d
1418

19+
down:
20+
docker compose --env-file .env.local down

backend/user/.env.compose

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# When run with compose
2+
EXPRESS_ENV="compose"
3+
PEERPREP_UI_HOST="http://frontend:3000" # to be changed
4+
5+
EXPRESS_PORT=9001
6+
EXPRESS_DB_HOST="user-db"
7+
EXPRESS_DB_PORT=5432
8+
POSTGRES_DB="user"
9+
POSTGRES_USER="peerprep-user-express"
10+
POSTGRES_PASSWORD="69/X8JxtAVsM+0YHT4RR5D7Ahf7bTobI4EED64FrzIU="
11+
PGDATA="/data/user-db"
12+
13+
EXPRESS_JWT_SECRET_KEY="jd+9qlXA0a3YsmVf2KJgyiJ3SprIR318IAwhRXck4Y8="
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
EXPRESS_ENV="local"
2+
PEERPREP_UI_HOST="http://localhost:5173"
3+
4+
EXPRESS_PORT=9001
5+
6+
# When run with standalone build
7+
EXPRESS_DB_HOST="host.docker.internal"
8+
9+
EXPRESS_DB_PORT=5431
10+
POSTGRES_DB="user"
11+
POSTGRES_USER="peerprep-user-express"
12+
POSTGRES_PASSWORD="69/X8JxtAVsM+0YHT4RR5D7Ahf7bTobI4EED64FrzIU="
13+
PGDATA="/data/user-db"
14+
15+
EXPRESS_JWT_SECRET_KEY="jd+9qlXA0a3YsmVf2KJgyiJ3SprIR318IAwhRXck4Y8="

backend/user/README.md

Lines changed: 19 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -1,140 +1,29 @@
1-
# Template Service
1+
# User Service
22

3-
This directory contains the code for the Template
4-
Service.
3+
## Running with Docker (Standalone)
54

6-
## Database
7-
8-
We use:
9-
10-
- PostgreSQL 16 for the database. To run it, we use:
11-
- Docker to run the database, as well as inject any user-defined
12-
configurations or SQL files into the Docker image.
13-
- Docker-Compose to run the database, as well as any other
14-
services this API microservice may depend on.
15-
- [**Drizzle**](https://orm.drizzle.team/) for the ORM.
16-
17-
Follow the instructions below for the setup, as well as to learn how to work with the database.
18-
19-
### Setup
20-
21-
1. Install Docker Desktop on your device. Launch it.
22-
23-
2. To verify that it is launched and installed correctly, run the
24-
following in your terminal:
25-
26-
```bash
27-
docker --version
28-
```
29-
30-
If the command does not error, and outputs a version, proceed to
31-
the next step.
32-
33-
3. Inspect the `docker-compose.yml` file. It
34-
should look like this:
35-
36-
```yml
37-
services:
38-
# ...
39-
postgres:
40-
# ...
41-
volumes:
42-
- "template-db-docker:/data/template-db"
43-
# - ./init.sql:/docker-entrypoint-initdb.d/init.sql
44-
ports:
45-
- "5431:5432"
46-
restart: unless-stopped
47-
48-
volumes:
49-
template-db-docker:
50-
external: true
51-
```
52-
53-
We observe that this Database relies on a
54-
Docker Volume. Replace all instances of
55-
`template-db-docker` with your desired
56-
volume name.
57-
58-
4. Then, create the Docker Volume with
59-
the following command:
60-
61-
```bash
62-
# in this case, the command is
63-
# docker volume create template-db-docker
64-
docker volume create <volume-name>
65-
```
66-
5. Finally, create the Database Container:
67-
68-
```bash
69-
docker-compose up -d
5+
1. Run this command to build:
6+
```sh
7+
docker build \
8+
-t user-express-local
9+
--build-arg env=local-docker-standalone \
10+
--build-arg port=9001 \
11+
-f express.Dockerfile .
7012
```
71-
72-
6. To bring it down, run this command:
73-
74-
```bash
75-
docker-compose down
13+
2. Run this command, from the root folder:
14+
```sh
15+
make db-up
7616
```
7717

78-
### Schema
79-
80-
We maintain the schema in the `src/lib/db/schema.ts` file.
81-
82-
Refer to the Drizzle documentation to learn how
83-
to properly define schemas. Then, insert your
84-
schemas into the file.
85-
86-
### Migration
87-
88-
After you have created/updated your schemas in
89-
the file, persist them to the Database with
90-
Migrations.
91-
92-
1. Configure your credentials (port,
93-
password, ...) in:
94-
95-
- `drizzle.config.ts`
96-
- `drizzle.migrate.mts`.
97-
- `src/lib/db/index.ts`.
18+
3. Run the necessary migrate and seed commands, if you haven't yet.
9819
99-
In the future, we may wish to migrate these
100-
credentials to environment variables.
101-
102-
2. Run the `npm run db:generate` command to
103-
generate your `.sql` Migration Files under the
104-
`drizzle` folder.
105-
106-
3. Rename your
107-
`<migration_num>_<random_name>.sql` file
108-
to `<migration_num>_<meaningful_name>.sql`.
109-
110-
For example:
111-
- Generated: `0000_dazzling_squirrel.sql`
112-
- Renamed: `0000_initial_schema.sql`.
113-
114-
Then, rename the
115-
`meta/_journal.json` tag from
116-
`0000_dazzling_squirrel` to
117-
`0000_initial_schema` as well. Replace the
118-
migration number and name with the one you
119-
used.
120-
121-
4. Finally, run the migration with this:
122-
123-
```bash
124-
npm run db:migrate
20+
4. Run this command to expose the container:
21+
```sh
22+
docker run -p 9001:9001 user-express-local
12523
```
12624
127-
### Connecting with the DB
25+
## Running with Docker-Compose (Main config)
12826
129-
1. Import the `db` instance from `lib/db`.
130-
2. Use the Drizzle APIs and the tables defined in
131-
`src/lib/schema.ts` to interact with the
132-
tables.
27+
Edit the variables in the `.env.compose` file and run `make up` from the root folder.
13328
134-
```ts
135-
import { db, tableName } from '../lib/db';
136-
137-
const route = async (req, res) => {
138-
await db.select().from(tableName); //...
139-
}
140-
```
29+
Any startup instructions will be run from `entrypoint.sh` instead.

backend/user/entrypoint.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/sh
2+
3+
if [ ! -f .db-init ]; then
4+
echo "migrating..."
5+
npm run db:prod:migrate
6+
echo "seeding..."
7+
npm run db:prod:seed
8+
touch .db-init
9+
fi
10+
11+
npm run start

backend/user/express.Dockerfile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,18 @@ FROM node:lts-alpine AS build
22
WORKDIR /data/question-express
33
COPY package*.json ./
44
RUN npm install
5+
ARG env
56
COPY . .
7+
COPY ".env.${env}" .env
68
RUN npm run build
79

810
FROM node:lts-alpine AS production
911
WORKDIR /data/question-express
1012
COPY --from=build /data/question-express/package*.json ./
1113
RUN npm ci --omit=dev
1214
COPY --from=build --chown=node:node /data/question-express/dist ./dist
15+
COPY --from=build /data/question-express/.env .env
1316

14-
ARG env
15-
COPY ".env.${env}" .
1617
ARG port
1718
EXPOSE ${port}
1819
CMD [ "npm", "run", "start" ]

backend/user/package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,17 @@
44
"main": "dist/index.js",
55
"scripts": {
66
"dev": "env-cmd -f .env.local nodemon src/index.ts | pino-pretty",
7-
"build": "env-cmd -f .env.local tsc && tsc-alias",
8-
"start": "env-cmd -f .env.local node dist/index.js",
7+
"build": "env-cmd -f .env tsc && tsc-alias",
8+
"start": "env-cmd -f .env node dist/index.js",
9+
"build:local": "env-cmd -f .env.local tsc && tsc-alias",
10+
"start:local": "env-cmd -f .env.local node dist/index.js",
911
"build:prod": "env-cmd -f .env.prod tsc && tsc-alias",
1012
"start:prod": "env-cmd -f .env.local node dist/index.js",
1113
"db:generate": "env-cmd -f .env.local drizzle-kit generate",
1214
"db:migrate": "env-cmd -f .env.local tsx ./src/lib/db/migrate.ts",
1315
"db:seed": "env-cmd -f .env.local tsx ./src/lib/db/seed.ts",
16+
"db:prod:migrate": "env-cmd -f .env tsx ./src/lib/db/migrate.ts",
17+
"db:prod:seed": "env-cmd -f .env tsx ./src/lib/db/seed.ts",
1418
"db:inspect": "env-cmd -f .env.local drizzle-kit studio",
1519
"fmt": "prettier --config .prettierrc src --write",
1620
"test": "echo \"Error: no test specified\" && exit 1"

docker-compose.local.yaml

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Command: docker-compose --env-file .env.local -f docker-compose.local.yaml up -d
2+
3+
services:
4+
user-db:
5+
hostname: "user-db"
6+
image: postgres:16.4
7+
container_name: "user-db"
8+
build:
9+
context: ./backend/user/src/lib/db
10+
env_file:
11+
- ./backend/user/.env.local
12+
volumes:
13+
- "user-db-docker:${USER_PGDATA}"
14+
ports:
15+
- "${USER_EXPRESS_DB_PORT}:5432"
16+
restart: unless-stopped
17+
18+
collab-db:
19+
hostname: "collab-db"
20+
image: "postgres:16.4"
21+
container_name: "collab-db"
22+
build:
23+
context: ./backend/collaboration/src/lib/db
24+
env_file:
25+
- ./backend/collaboration/.env.local
26+
volumes:
27+
- "collab-db-docker:/${COLLAB_PGDATA}"
28+
# - ./init.sql:/docker-entrypoint-initdb.d/init.sql
29+
ports:
30+
- "${COLLAB_EXPRESS_DB_PORT}:5432"
31+
restart: unless-stopped
32+
33+
question-db:
34+
hostname: "question-db"
35+
image: postgres:16.4
36+
container_name: "question-db"
37+
build:
38+
context: ./backend/question/src/lib/db
39+
env_file:
40+
- ./backend/question/.env.local
41+
volumes:
42+
- "question-db-docker:${QUESTION_PGDATA}"
43+
# - ./init.sql:/docker-entrypoint-initdb.d/init.sql
44+
ports:
45+
- "${QUESTION_EXPRESS_DB_PORT}:5432"
46+
restart: unless-stopped
47+
48+
volumes:
49+
user-db-docker:
50+
external: true
51+
collab-db-docker:
52+
external: true
53+
question-db-docker:
54+
external: true

docker-compose.yaml

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@ services:
1111
- ./backend/user/.env.local
1212
volumes:
1313
- "user-db-docker:${USER_PGDATA}"
14-
# - ./init.sql:/docker-entrypoint-initdb.d/init.sql
1514
ports:
1615
- "${USER_EXPRESS_DB_PORT}:5432"
1716
restart: unless-stopped
17+
networks:
18+
- user-network
1819

1920
collab-db:
2021
hostname: "collab-db"
@@ -46,10 +47,44 @@ services:
4647
- "${QUESTION_EXPRESS_DB_PORT}:5432"
4748
restart: unless-stopped
4849

50+
user-service:
51+
image: "user-express"
52+
container_name: "user-express"
53+
build:
54+
context: ./backend/user
55+
dockerfile: ./express.Dockerfile
56+
target: build
57+
args:
58+
# For building with the correct env vars
59+
- env=${USER_EXPRESS_ENV}
60+
- port=${USER_EXPRESS_PORT}
61+
ports:
62+
- "9001:${USER_EXPRESS_PORT}"
63+
env_file:
64+
- ./backend/user/.env.compose
65+
environment:
66+
# Docker Compose Specific
67+
- EXPRESS_DB_HOST=user-db
68+
- EXPRESS_DB_PORT=5432
69+
volumes:
70+
- user-service:/data/question-express
71+
depends_on:
72+
- user-db
73+
networks:
74+
- user-network
75+
entrypoint: ./entrypoint.sh
76+
4977
volumes:
78+
user-service:
79+
5080
user-db-docker:
5181
external: true
5282
collab-db-docker:
5383
external: true
5484
question-db-docker:
5585
external: true
86+
87+
88+
networks:
89+
user-network:
90+
driver: bridge

0 commit comments

Comments
 (0)