Skip to content

Commit

Permalink
Merge pull request #20 from xenosf/docker-compose-env
Browse files Browse the repository at this point in the history
Centralize env config
  • Loading branch information
mayuanxin1234 authored Oct 6, 2024
2 parents 023fb79 + 977cd9a commit 1d31187
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 37 deletions.
25 changes: 25 additions & 0 deletions server/.env.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
##################
# BACKEND CONFIG #
##################

# !NOTE: This is a template. Make a copy named .env and fill in the relevant fields.

# COMMON
# ---------------
ENV=PROD


# USER SERVICE
# ---------------
PORT_USER=3001
# MongoDB
DB_URI_USER=''
# JWT secret
JWT_SECRET=you-can-replace-this-with-your-own-secret


# QUESTION SERVICE
# ---------------
PORT_QUESTION=3002
# MongoDB
DB_URI_QUESTION=''
12 changes: 10 additions & 2 deletions server/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
# Backend
# Back-end

The back-end has been split into multiple containerized microservices. You can run all of them at once using `docker compose`:
The back-end has been split into multiple containerized microservices.

## Setup

1. Make a copy of `.env.template` called `.env` and fill in the variables (or add a pre-filled env file)

## Running

You can run all the services using `docker compose`:

```sh
docker compose build
Expand Down
24 changes: 20 additions & 4 deletions server/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
services:
user:
build: ./user-service
build:
context: ./user-service
dockerfile: Dockerfile
args:
- PORT=${PORT_USER}
ports:
- 3001:3001
- ${PORT_USER}:${PORT_USER}
environment:
- ENV
- DB_CLOUD_URI=${DB_URI_USER}
- DB_LOCAL_URI=${DB_URI_USER}
- JWT_SECRET

questions:
build: ./question-service
build:
context: ./question-service
dockerfile: Dockerfile
args:
- PORT=${PORT_QUESTION}
ports:
- 3002:3002
- ${PORT_QUESTION}:${PORT_QUESTION}
environment:
- ENV
- DB_CLOUD_URI=${DB_URI_QUESTION}
11 changes: 0 additions & 11 deletions server/question-service/.env.template

This file was deleted.

6 changes: 3 additions & 3 deletions server/question-service/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# syntax=docker/dockerfile:1

FROM node:18-alpine
ARG PORT
ENV PORT=$PORT
WORKDIR /app
COPY . .
RUN npm install --omit=dev
CMD ["npm", "start"]
EXPOSE 3002
EXPOSE $PORT
14 changes: 0 additions & 14 deletions server/user-service/.env.template

This file was deleted.

6 changes: 3 additions & 3 deletions server/user-service/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# syntax=docker/dockerfile:1

FROM node:18-alpine
ARG PORT
ENV PORT=$PORT
WORKDIR /app
COPY . .
RUN npm install --omit=dev
CMD ["npm", "start"]
EXPOSE 3001
EXPOSE $PORT

0 comments on commit 1d31187

Please sign in to comment.