diff --git a/README.md b/README.md index a4500f4..0bfd445 100644 --- a/README.md +++ b/README.md @@ -147,6 +147,38 @@ npm run dev --- +## Docker Deployment + +To run the entire application using Docker Compose: + +### Prerequisites +- Docker and Docker Compose installed +- Same environment variables as local development + +### Setup +1. Copy and configure environment file: + ```bash + cp server/.env.example server/.env + ``` + Fill in the values as described in step 3 of Local Development. + +2. Build and run: + ```bash + docker-compose up --build + ``` + +3. Access the app: + - Frontend: http://localhost:3000 + - Backend: http://localhost:5000 + - MongoDB: localhost:27017 + +4. Stop the containers: + ```bash + docker-compose down + ``` + +--- + ## Common Issues & Troubleshooting - CORS error in browser console: diff --git a/client/.dockerignore b/client/.dockerignore new file mode 100644 index 0000000..631c59c --- /dev/null +++ b/client/.dockerignore @@ -0,0 +1,6 @@ +node_modules +npm-debug.log +dist +.env +.git +.gitignore \ No newline at end of file diff --git a/client/Dockerfile b/client/Dockerfile new file mode 100644 index 0000000..1a5adc2 --- /dev/null +++ b/client/Dockerfile @@ -0,0 +1,22 @@ +# Build stage +FROM node:18 AS build + +ARG VITE_SOCKET_URL +ENV VITE_SOCKET_URL=$VITE_SOCKET_URL + +WORKDIR /app + +COPY package*.json ./ +RUN npm install + +COPY . . +RUN npm run build + +# Production stage +FROM nginx:alpine + +COPY --from=build /app/dist /usr/share/nginx/html + +EXPOSE 80 + +CMD ["nginx", "-g", "daemon off;"] \ No newline at end of file diff --git a/client/src/socket.js b/client/src/socket.js index 7b68557..fdf7edc 100644 --- a/client/src/socket.js +++ b/client/src/socket.js @@ -1,6 +1,6 @@ import { io } from "socket.io-client"; -const socket = io("https://guessync.onrender.com/", { +const socket = io(import.meta.env.VITE_SOCKET_URL || "https://guessync.onrender.com/", { autoConnect: false, reconnection: true, reconnectionAttempts: Infinity, diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..e99dfe0 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,37 @@ +version: '3.8' + +services: + client: + build: + context: ./client + dockerfile: Dockerfile + args: + - VITE_SOCKET_URL=http://server:5000 + ports: + - "3000:80" + depends_on: + - server + + server: + build: + context: ./server + dockerfile: Dockerfile + ports: + - "5000:5000" + env_file: + - ./server/.env + environment: + - NODE_ENV=production + - MONGO_URI=mongodb://mongo:27017/guessync + depends_on: + - mongo + + mongo: + image: mongo:5.0 + ports: + - "27017:27017" + volumes: + - mongo_data:/data/db + +volumes: + mongo_data: \ No newline at end of file diff --git a/server/.dockerignore b/server/.dockerignore new file mode 100644 index 0000000..94f6d34 --- /dev/null +++ b/server/.dockerignore @@ -0,0 +1,5 @@ +node_modules +npm-debug.log +.env +.git +.gitignore \ No newline at end of file diff --git a/server/package.json b/server/package.json index d528533..47ef6f1 100644 --- a/server/package.json +++ b/server/package.json @@ -5,6 +5,7 @@ "main": "index.js", "scripts": { "dev": "nodemon server.js", + "start": "node server.js", "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [],