diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..7c12d6d4 --- /dev/null +++ b/Dockerfile @@ -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"] \ No newline at end of file diff --git a/README.md b/README.md index e92a70d2..1edbcc58 100644 --- a/README.md +++ b/README.md @@ -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 :) diff --git a/docker-compose.dev.yaml b/docker-compose.dev.yaml new file mode 100644 index 00000000..cf6b5965 --- /dev/null +++ b/docker-compose.dev.yaml @@ -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: diff --git a/docker-compose.prod.yaml b/docker-compose.prod.yaml new file mode 100644 index 00000000..bbef2202 --- /dev/null +++ b/docker-compose.prod.yaml @@ -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: diff --git a/server/Dockerfile.server b/server/Dockerfile.server new file mode 100644 index 00000000..f2510041 --- /dev/null +++ b/server/Dockerfile.server @@ -0,0 +1,7 @@ +FROM node:20 +WORKDIR /app +COPY package*.json ./ +RUN npm install +COPY . . +RUN cp env.example .env +CMD ["npm", "start"] \ No newline at end of file diff --git a/server/env.example b/server/env.example new file mode 100644 index 00000000..f412bbff --- /dev/null +++ b/server/env.example @@ -0,0 +1,4 @@ +JWT_SECRET_KEY=secret + +DATABASE=fitbody_mongo # "fitbody_mongo" (for Docker Contrainer) +# DATABASE=localhost # "localhost" (for Local Server) \ No newline at end of file diff --git a/server/index.js b/server/index.js index cd765d31..b84d8934 100644 --- a/server/index.js +++ b/server/index.js @@ -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 diff --git a/vite.config.js b/vite.config.js index 2dea53a3..18b385d9 100644 --- a/vite.config.js +++ b/vite.config.js @@ -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", + }, }) \ No newline at end of file