Skip to content

Commit 26ac65f

Browse files
authored
Dockerize user-db locally (#53)
1 parent c9cec7b commit 26ac65f

File tree

5 files changed

+43
-20
lines changed

5 files changed

+43
-20
lines changed

.env.sample

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@ QUESTION_DB_USERNAME=user
88
QUESTION_DB_PASSWORD=password
99

1010
# User Service
11-
USER_SERVICE_CLOUD_URI=mongodb+srv://admin:<db_password>@cluster0.uo0vu.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0
12-
USER_SERVICE_LOCAL_URI=mongodb://127.0.0.1:27017/peerprepUserServiceDB
13-
14-
# Will use cloud MongoDB Atlas database
15-
ENV=PROD
11+
USER_DB_CLOUD_URI=<FILL-THIS-IN>
12+
USER_DB_LOCAL_URI=mongodb://user-db:27017/user
13+
USER_DB_USERNAME=user
14+
USER_DB_PASSWORD=password
1615

1716
# Secret for creating JWT signature
1817
JWT_SECRET=you-can-replace-this-with-your-own-secret

compose.dev.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,8 @@ services:
1818
command: npm run dev
1919
volumes:
2020
- /app/node_modules
21-
- ./services/user:/app
21+
- ./services/user:/app
22+
23+
user-db:
24+
ports:
25+
- 27018:27017

compose.yml

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,34 @@ services:
4949
ports:
5050
- 8082:8082
5151
environment:
52-
USER_SERVICE_CLOUD_URI: ${USER_SERVICE_CLOUD_URI}
53-
USER_SERVICE_LOCAL_URI: ${USER_SERVICE_LOCAL_URI}
54-
ENV: ${ENV}
52+
DB_CLOUD_URI: ${USER_DB_CLOUD_URI}
53+
DB_LOCAL_URI: ${USER_DB_LOCAL_URI}
54+
DB_USERNAME: ${USER_DB_USERNAME}
55+
DB_PASSWORD: ${USER_DB_PASSWORD}
5556
JWT_SECRET: ${JWT_SECRET}
57+
networks:
58+
- user-db-network
59+
restart: always
60+
61+
user-db:
62+
container_name: user-db
63+
image: mongo:7.0.14
64+
environment:
65+
MONGO_INITDB_ROOT_USERNAME: ${USER_DB_USERNAME}
66+
MONGO_INITDB_ROOT_PASSWORD: ${USER_DB_PASSWORD}
67+
volumes:
68+
- user-db:/data/db
69+
networks:
70+
- user-db-network
71+
command: --quiet
5672
restart: always
5773

5874
volumes:
5975
question-db:
76+
user-db:
6077

6178
networks:
6279
question-db-network:
80+
driver: bridge
81+
user-db-network:
6382
driver: bridge

services/user/.env.sample

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
# User Service
2-
USER_SERVICE_CLOUD_URI=<cloud_uri>
3-
USER_SERVICE_LOCAL_URI=mongodb://127.0.0.1:27017/peerprepUserServiceDB
4-
PORT=8082
5-
6-
# Will use cloud MongoDB Atlas database
7-
ENV=PROD
1+
# This is a sample environment configuration file.
2+
# Copy this file to .env and replace the placeholder values with your own.
3+
DB_CLOUD_URI=<FILL-THIS-IN>
4+
DB_LOCAL_URI=mongodb://user-db:27017/user
5+
PORT=8083
86

97
# Secret for creating JWT signature
108
JWT_SECRET=you-can-replace-this-with-your-own-secret

services/user/src/model/repository.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@ import 'dotenv/config';
33
import { connect } from 'mongoose';
44

55
export async function connectToDB() {
6-
const mongoDBUri =
7-
process.env.ENV === 'PROD' ? process.env.USER_SERVICE_CLOUD_URI : process.env.USER_SERVICE_LOCAL_URI;
6+
const mongoUri = process.env.NODE_ENV === 'production' ? process.env.DB_CLOUD_URI : process.env.DB_LOCAL_URI;
87

9-
if (!mongoDBUri) {
8+
if (!mongoUri) {
109
throw new Error('MongoDB URI not specified');
1110
}
1211

13-
await connect(mongoDBUri);
12+
await connect(mongoUri, {
13+
authSource: 'admin',
14+
user: process.env.DB_USERNAME,
15+
pass: process.env.DB_PASSWORD,
16+
});
1417
}
1518

1619
export async function createUser(username: string, email: string, password: string) {

0 commit comments

Comments
 (0)