Skip to content

Commit

Permalink
Merge pull request #46 from CS3219-AY2324S1/Assignment4
Browse files Browse the repository at this point in the history
Assignment4
  • Loading branch information
ryanchua00 authored Oct 9, 2023
2 parents 6474e45 + 63cb1d0 commit 7a7c288
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 1 deletion.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Getting Started with Docker for Development

1. Install docker at https://www.docker.com/

2. Start docker

3. Run `docker compose build` to build the images and containers

4. Run `docker compose up` to start the container (Note: this will only start the user & question service)

5. Note: this does not start the api-gateway. Follow the relevant `./gateway/README.md`

## Developer guide

As of the last update of this README, only the user-service and question-service have been dockerized. A respective `Dockerfile` has been created within each of their folders. This `Dockerfile` specifies the instructions in order to build the container, install the necessary node modules etc.

To simplify the container building, a `docker-compose.yml` has been created in order to start both containers at once. This is achieved through running `docker compose build` and `docker compose up` to call both dockerfiles.

The api-gateway requires more work, see https://dev.to/naseef012/create-a-microservices-app-with-dockerized-express-api-gateway-1kf9 for more information.

## Finishing Development

When you are done with developing, kill the program (Ctrl^C) and run `docker compose down`. This cleans up local resources used by docker.

Note: For Mac users, this doesn't work and volumes will continue to pile up. Run `docker system prune -a --volumes` occasionally to remove the remaining volumes.
21 changes: 21 additions & 0 deletions backend/user-service/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Referenced from https://nodejs.org/en/docs/guides/nodejs-docker-webapp
FROM node:20

# Create app directory
WORKDIR /app

# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY ./backend/user-service/package*.json .
RUN npm install
# If you are building your code for production
# RUN npm ci --omit=dev

COPY ./backend/user-service .

EXPOSE 3001

RUN npx prisma generate --schema /app/prisma/schema.prisma

CMD [ "npm", "start" ]
1 change: 1 addition & 0 deletions backend/user-service/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

generator client {
provider = "prisma-client-js"
binaryTargets = ["native", "linux-arm64-openssl-3.0.x"]
}

datasource db {
Expand Down
22 changes: 22 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
version: '3.8'

services:
users:
container_name: user-service
build:
context: .
dockerfile: ./backend/user-service/Dockerfile
ports:
- "3001:3001"
env_file:
- ./backend/user-service/.env

questions:
container_name: question-service
build:
context: .
dockerfile: ./mongodb-database/Dockerfile
ports:
- "3002:3002"
env_file:
- ./mongodb-database/.env
20 changes: 20 additions & 0 deletions mongodb-database/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Referenced from https://nodejs.org/en/docs/guides/nodejs-docker-webapp
FROM node:20

# Create app directory
WORKDIR /app

# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY ./mongodb-database/package*.json .
RUN npm install

# If you are building your code for production
# RUN npm ci --omit=dev

COPY ./mongodb-database .

EXPOSE 3002

CMD [ "npm", "start" ]
3 changes: 2 additions & 1 deletion mongodb-database/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"dotenv": "^16.3.1",
"express": "^4.18.2",
"mongodb": "^6.1.0",
"mongoose": "^7.5.3"
"mongoose": "^7.5.3",
"nodemon": "^3.0.1"
}
}

0 comments on commit 7a7c288

Please sign in to comment.