Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Containerisation #425

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM node:20 as Base
WORKDIR /app
COPY package*.json ./
RUN npm install

FROM base AS development
COPY . .
CMD ["npm", "run", "dev"]

FROM base AS production
COPY . .
RUN npm run build
CMD ["npm", "run","preview"]
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,17 @@ Here is the live view of this website. It is hosted on Vercel https://fit-body-d

3. Run `npm start` for starting the server (FitBody is currently running on `localhost:3000`)

#### Using Docker Container

1. At root directory, Run the Docker Compose up Command

```bash
docker-compose -f docker-compose.dev.yaml up
# or
docker-compose -f docker-compose.dev.yaml up --build
```

Your Docker container will be running at `https://localhost:8000/`.


Happy coding :)
Expand Down
33 changes: 33 additions & 0 deletions docker-compose.dev.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
version: "3.8"

services:
fitbody_web:
container_name: FitBody_Web
build:
context: .
dockerfile: Dockerfile
target: development
ports:
- "8000:8000"
volumes:
- .:/app

fitbody_server:
container_name: FitBody_Server
build:
context: ./server
dockerfile: Dockerfile.server
ports:
- "3000:3000"
depends_on:
- fitbody_mongo

fitbody_mongo:
container_name: "FitBody_Mongo"
image: mongo
ports:
- "27017:27017"
volumes:
- mongodb-data:/data/db/
volumes:
mongodb-data:
33 changes: 33 additions & 0 deletions docker-compose.prod.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
version: "3.8"

services:
fitbody_web:
container_name: FitBody_Web
build:
context: .
dockerfile: Dockerfile
target: production
ports:
- "8080:8080"
volumes:
- .:/app

fitbody_server:
container_name: FitBody_Server
build:
context: ./server
dockerfile: Dockerfile.server
ports:
- "3000:3000"
depends_on:
- mongo

mongo:
container_name: "mongodb"
image: mongo
ports:
- "27017:27017"
volumes:
- mongodb-data:/data/db/
volumes:
mongodb-data:
7 changes: 7 additions & 0 deletions server/Dockerfile.server
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM node:20
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN cp env.example .env
CMD ["npm", "start"]
4 changes: 4 additions & 0 deletions server/env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
JWT_SECRET_KEY=secret

DATABASE=fitbody_mongo # "fitbody_mongo" (for Docker Contrainer)
# DATABASE=localhost # "localhost" (for Local Server)
18 changes: 10 additions & 8 deletions server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,16 @@ server.use((obj, req, res, next) => {

// Database connection Function
const ConnetMongoDB = async () => {
try {
await mongoose.connect('mongodb://localhost:27017/fitbody');
console.log('DB connected !');
} catch (error) {
console.error('Error: ', error);
console.log('DB connection failed');
}
}
try {
await mongoose.connect(
`mongodb://${process.env.DATABASE}:27017/fitbody`
);
console.log("DB connected !");
} catch (error) {
console.error("Error: ", error);
console.log("DB connection failed");
}
};


// Server Listening
Expand Down
18 changes: 18 additions & 0 deletions vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,22 @@ import react from '@vitejs/plugin-react'
// https://vitejs.dev/config/
export default defineConfig({
plugins: [react()],
build: {
outDir: "build",
port: 8080,
strictPort: true,
host: true,
origin: "http://0.0.0.0:8080",
},
preview: {
port: 8080,
host: true,
origin: "http://0.0.0.0:8080",
},
server: {
port: 8000,
strictPort: true,
host: true,
origin: "http://0.0.0.0:8000",
},
})
Loading