Skip to content

Commit b5382f8

Browse files
committed
Fixed the project to run using docker compose
1 parent 277d222 commit b5382f8

File tree

5 files changed

+65
-132
lines changed

5 files changed

+65
-132
lines changed

README.md

+26-41
Original file line numberDiff line numberDiff line change
@@ -11,52 +11,24 @@ The project is divided into two main directories:
1111
- `/client` - Contains the Next.js frontend application.
1212
- `/server` - Contains the Express.js backend server, which uses Prisma for ORM and PostgreSQL as the database.
1313

14-
## Prerequisites
15-
16-
Before you begin, ensure you have installed:
17-
18-
- Node.js and npm
19-
- PostgreSQL
20-
21-
## Installation
14+
## RUNNING THE PROJECT USING DOCKER COMPOSE (RECCOMANDED)
2215

2316
1. Clone the repository:
2417

2518
```bash
2619
git clone https://github.com/definecoder/CS24-p2-define-coders.git
2720
```
28-
29-
2. Install the dependencies in both the `/client` and `/server` directories:
30-
31-
```bash
32-
cd EcoSync/client
33-
npm install
34-
35-
cd ../server
36-
npm install
37-
```
38-
39-
## Configuration
40-
41-
In the `/server` & `/client` directory, rename `.env.example` to `.env` and fill in your PostgreSQL database details `with you postgres password e.g: "postgresql://postgres:YOURPASSWORDHERE@127.0.0.1:5432/ecosync"` and other environment variables from [this google doc link](https://docs.google.com/document/d/1j1UFD3U4ejqeDRb26N9WffqvaLzwYYAxGY2OaWlZTO4/edit?usp=sharing).
42-
43-
## Running the Application
44-
45-
1. Start the backend server:
46-
21+
2. Open the source directory in a terminal and give the following command
4722
```bash
48-
cd EcoSync/server
49-
npm run dev
23+
docker compose up --build
5024
```
25+
wait for the docker compose to complete and then frontend application will be available running at `http://localhost:3000`, and the backend server will be running at `http://localhost:8585` (or whatever port you specified).
5126

52-
2. In a new terminal window, start the frontend application:
53-
27+
3. After backend and frontend services are running again open the source directory in `another terminal` and give the following command
5428
```bash
55-
cd EcoSync/client
56-
npm run dev
29+
docker exec -it backend /bin/bash -c "cd ./src && npx prisma migrate dev --name init && npx prisma db seed 2> /dev/null || echo \'Database is already seeded\'"
5730
```
58-
59-
The frontend application will be available at `http://localhost:3000`, and the backend server will be running at `http://localhost:8585` (or whatever port you specified).
31+
This will do the prisma migration and run the seed file to push initial data.
6032

6133
## Credentials set by the initial db migration
6234

@@ -76,17 +48,30 @@ The frontend application will be available at `http://localhost:3000`, and the b
7648
}
7749
}
7850
```
79-
## To run using Docker
80-
Go to the source directory and give the following command
81-
```bash
82-
docker compose up --build
83-
```
84-
## To run using AWS Backend (if docker compose fails)
51+
52+
## RUNNING THE BACKEND USING AWS (IF DOCKER COMPOSE FAILS!)
8553
Go to `client\data\apiRoutes` and comment the `first line` and uncomment the second
8654
```js
8755
export const baseUrl = "http://localhost:8585"; // Uncomment to run Locally
8856
// export const baseUrl = "http://13.250.36.61"; // Uncomment to run in AWS
8957
```
58+
## CONFIGURATION
59+
In the `/server` & `/client` directory, rename `.env.example` to `.env` and fill in your PostgreSQL database details `with you postgres password e.g: "postgresql://postgres:YOURPASSWORDHERE@127.0.0.1:5432/ecosync"` and other environment variables from [this google doc link](https://docs.google.com/document/d/1j1UFD3U4ejqeDRb26N9WffqvaLzwYYAxGY2OaWlZTO4/edit?usp=sharing).
60+
61+
## RUNNING FRONTEND
62+
Run the frontend using docker:
63+
```bash
64+
docker compose up --build
65+
```
66+
or locally by running following command:
67+
```bash
68+
cd .\client\
69+
npm i
70+
npm run dev
71+
```
72+
73+
74+
## NOTE ON USING AWS BACKEND
9075
When running on AWS the frontend might face some slow network problems which can be fixed by switching speed from no thorttling to fast 3G. [You can check this video to see the demonstration of the problem mentioned here](https://drive.google.com/drive/folders/1B5N5o0ms7mizSYm5HNiAjJj0O82Zb1tq?usp=sharing).
9176

9277
## License

client/Dockerfile

+3-74
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,13 @@
1+
FROM node:latest
12

2-
# FROM node:latest
3+
WORKDIR /app
34

4-
# WORKDIR /usr/src/app
5-
6-
# COPY package*.json ./
7-
8-
# RUN npm install
9-
10-
# COPY . .
11-
12-
# EXPOSE 3000
13-
14-
15-
# CMD ["npm", "run", "dev"]
16-
17-
#if not works
18-
#-------------------
19-
20-
# FROM node:18-alpine as base
21-
# RUN apk add --no-cache g++ make py3-pip libc6-compat
22-
# WORKDIR /app
23-
# COPY package*.json ./
24-
# EXPOSE 3000
25-
26-
# FROM base as builder
27-
# WORKDIR /app
28-
# COPY . .
29-
# RUN npm run build
30-
31-
32-
# FROM base as production
33-
# WORKDIR /app
34-
35-
# ENV NODE_ENV=production
36-
# RUN npm ci
37-
38-
# RUN addgroup -g 1001 -S nodejs
39-
# RUN adduser -S nextjs -u 1001
40-
# USER nextjs
41-
42-
43-
# COPY --from=builder --chown=nextjs:nodejs /app/.next ./.next
44-
# COPY --from=builder /app/node_modules ./node_modules
45-
# COPY --from=builder /app/package.json ./package.json
46-
# COPY --from=builder /app/public ./public
47-
48-
# CMD npm start
49-
50-
# FROM base as dev
51-
# ENV NODE_ENV=development
52-
# RUN npm install
53-
# COPY . .
54-
# CMD npm run dev
55-
56-
57-
# Use the Ubuntu base image
58-
# Use the Ubuntu base image
59-
FROM ubuntu:latest
60-
61-
# Install necessary packages for Node.js and npm
62-
RUN apt-get update && \
63-
apt-get install -y curl && \
64-
curl -fsSL https://deb.nodesource.com/setup_20.x | bash - && \
65-
apt-get install -y nodejs && \
66-
apt-get clean
67-
68-
# Set working directory
69-
WORKDIR /usr/src/app
70-
71-
# Copy package.json and package-lock.json
725
COPY package*.json ./
736

74-
# Install project dependencies
75-
RUN npm install --force
7+
RUN npm install
768

77-
# Copy the rest of the application files
789
COPY . .
7910

80-
# Expose port 3000
8111
EXPOSE 3000
8212

83-
# Command to run the Next.js project
8413
CMD ["npm", "run", "dev"]

docker-compose.yml

+31-11
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
version: '3'
2-
services:
3-
postgres:
2+
services:
3+
db:
4+
container_name: db
45
image: postgres
6+
restart: always
7+
environment:
8+
POSTGRES_PASSWORD: password123
9+
POSTGRES_USER: user123
10+
POSTGRES_DB: db123
11+
512
ports:
613
- "5432:5432"
7-
environment:
8-
POSTGRES_USER: postgres
9-
POSTGRES_PASSWORD: postgres
10-
POSTGRES_DB: ecosync
11-
14+
volumes:
15+
- pgdata:/var/lib/postgresql/data
16+
1217
frontend:
1318
build:
1419
context: ./client
@@ -18,17 +23,32 @@ services:
1823
- "3000:3000"
1924
# volumes:
2025
# - ./client:/usr/src/app
21-
env_file:
22-
- .env
26+
environment:
27+
- NEXT_PUBLIC_GOOGLE_MAPS_API_KEY=AIzaSyD1HUzeHWo0hXLXknwRgZ_h8bwRIU-_OyU
28+
- NEXT_PUBLIC_RECAPTCHA_SITE_KEY=6Lcb-aYpAAAAADJLVict1FyCiey132R8qdWLon2w
29+
- RECAPTCHA_SECRET_KEY=6Lcb-aYpAAAAAL7hCRtE4OVhqDA6Qs15Lo8HUHx-
2330

2431
backend:
32+
container_name: backend
33+
image: backend
2534
build:
2635
context: ./server
2736
dockerfile: Dockerfile
2837
ports:
2938
- "8585:8585"
39+
40+
depends_on:
41+
- db
42+
3043
# volumes:
3144
# - ./server:/usr/src/app
32-
env_file:
33-
- .env
45+
environment:
46+
- PORT=8585
47+
- DATABASE_URL=postgresql://user123:password123@db:5432/db123?schema=public
48+
- JWT_SECRET=viugh@#%48rsdsdbov087qh798yh
49+
- MAIL_EMAIL=sohojthikadari@gmail.com
50+
- MAIL_PASSWORD=nlvobxoajkaicdhx
51+
- NEXT_PUBLIC_GOOGLE_MAPS_API_KEY=AIzaSyD1HUzeHWo0hXLXknwRgZ_h8bwRIU-_OyU
3452

53+
volumes:
54+
pgdata: {}

server/Dockerfile

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
21
FROM node:latest
32

4-
WORKDIR /usr/src/app
3+
WORKDIR /app
54

65
COPY package*.json ./
76

8-
RUN apt-get update && apt-get install -y postgresql-client
7+
RUN npm install
98

109
COPY . .
1110

11+
RUN cd ./src && npx prisma generate
12+
1213
EXPOSE 8585
1314

14-
CMD ["sh", "-c", "until pg_isready -h postgres -p 5432; do sleep 1; done && npm i && npm run dev"]
15-
# CMD ["npx", "nodemon"]
15+
CMD ["npm", "run", "dev"]

server/package.json

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
"description": "",
55
"main": "index.js",
66
"scripts": {
7-
"install": "cd ./src && npx prisma migrate dev --name init && createdb ecosync 2> /dev/null || echo \"database already exists\"",
87
"build": "tsc",
98
"start": "node build/index.js",
109
"dev": "ts-node src/index.ts",

0 commit comments

Comments
 (0)