From d525e8476d6392b557de576ff5b2be107fa1a009 Mon Sep 17 00:00:00 2001 From: Francesco Lorenzo D'Amico Date: Thu, 19 Mar 2026 11:27:14 +0100 Subject: [PATCH] Add Docker and Nginx config for frontend, backend, and MongoDB --- Backend/.dockerignore | 4 +++ Backend/Dockerfile | 15 +++++++++++ docker-compose.yml | 59 ++++++++++++++++++++++++++++++++++++++++++ frontend/.dockerignore | 4 +++ frontend/Dockerfile | 23 ++++++++++++++++ frontend/nginx.conf | 37 ++++++++++++++++++++++++++ 6 files changed, 142 insertions(+) create mode 100644 Backend/.dockerignore create mode 100644 Backend/Dockerfile create mode 100644 docker-compose.yml create mode 100644 frontend/.dockerignore create mode 100644 frontend/Dockerfile create mode 100644 frontend/nginx.conf diff --git a/Backend/.dockerignore b/Backend/.dockerignore new file mode 100644 index 0000000..69cfdb3 --- /dev/null +++ b/Backend/.dockerignore @@ -0,0 +1,4 @@ +node_modules +npm-debug.log +.env +data diff --git a/Backend/Dockerfile b/Backend/Dockerfile new file mode 100644 index 0000000..0f7b4f9 --- /dev/null +++ b/Backend/Dockerfile @@ -0,0 +1,15 @@ +FROM node:20-alpine + +WORKDIR /app + +COPY package*.json ./ +RUN npm ci --omit=dev + +COPY . . + +ENV NODE_ENV=production +ENV PORT=4000 + +EXPOSE 4000 + +CMD ["node", "index.js"] diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..4430181 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,59 @@ +services: + frontend: + image: francesco448/chat-app-frontend:latest + build: + context: ./frontend + args: + VITE_API_URL: "" + restart: unless-stopped + depends_on: + - backend + ports: + - "80:80" + networks: + - chat-network + + backend: + image: francesco448/chat-app-backend:latest + build: + context: ./Backend + restart: unless-stopped + depends_on: + mongo: + condition: service_healthy + environment: + - PORT=4000 + - CLIENT_ORIGIN=http://localhost + - DATA_STORE=mongo + - MONGO_URI=mongodb://${MONGO_ROOT_USER:-admin}:${MONGO_ROOT_PASSWORD:-chatpass}@mongo:27017/${MONGO_DB:-chatapp}?authSource=admin + - MONGO_DB=${MONGO_DB:-chatapp} + networks: + - chat-network + + mongo: + image: mongo:7 + container_name: chat-app-mongo + restart: unless-stopped + environment: + - MONGO_INITDB_ROOT_USERNAME=${MONGO_ROOT_USER:-admin} + - MONGO_INITDB_ROOT_PASSWORD=${MONGO_ROOT_PASSWORD:-chatpass} + - MONGO_INITDB_DATABASE=${MONGO_DB:-chatapp} + volumes: + - mongo-data:/data/db + ports: + - "27017:27017" + networks: + - chat-network + healthcheck: + test: ["CMD", "mongosh", "--eval", "db.adminCommand('ping')"] + interval: 10s + timeout: 5s + retries: 5 + +networks: + chat-network: + driver: bridge + +volumes: + mongo-data: + driver: local diff --git a/frontend/.dockerignore b/frontend/.dockerignore new file mode 100644 index 0000000..cd44c15 --- /dev/null +++ b/frontend/.dockerignore @@ -0,0 +1,4 @@ +node_modules +dist +npm-debug.log +.env diff --git a/frontend/Dockerfile b/frontend/Dockerfile new file mode 100644 index 0000000..ab67f07 --- /dev/null +++ b/frontend/Dockerfile @@ -0,0 +1,23 @@ +FROM node:20-alpine AS builder + +WORKDIR /app + +COPY package*.json ./ +RUN npm ci + +COPY . . + + +ARG VITE_API_URL="" +ENV VITE_API_URL=$VITE_API_URL + +RUN npm run build + +FROM nginx:alpine + +COPY --from=builder /app/dist /usr/share/nginx/html +COPY nginx.conf /etc/nginx/conf.d/default.conf + +EXPOSE 80 + +CMD ["nginx", "-g", "daemon off;"] diff --git a/frontend/nginx.conf b/frontend/nginx.conf new file mode 100644 index 0000000..c39a185 --- /dev/null +++ b/frontend/nginx.conf @@ -0,0 +1,37 @@ +server { + listen 80; + + # Serve the static React build + location / { + root /usr/share/nginx/html; + index index.html; + # Fallback to index.html for client-side routing + try_files $uri $uri/ /index.html; + } + + # Proxy REST API requests to the backend + location /rooms { + proxy_pass http://backend:4000; + proxy_http_version 1.1; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + } + + # Proxy the health check endpoint to the backend + location /health { + proxy_pass http://backend:4000; + proxy_http_version 1.1; + } + + # Proxy Socket.IO — requires WebSocket upgrade headers + location /socket.io/ { + proxy_pass http://backend:4000; + proxy_http_version 1.1; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection "upgrade"; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_cache_bypass $http_upgrade; + } +}