Skip to content
Merged
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
56 changes: 56 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Dependencies
node_modules/
npm-debug.log*
yarn-debug.log*
yarn-error.log*
# Note: package-lock.json is needed for npm ci in Docker

# Environment variables
.env
.env.local
.env.*.local

# Logs
logs/
*.log

# Testing
coverage/
.nyc_output/

# IDE and editor files
.vscode/
.idea/
*.swp
*.swo
*~
.DS_Store

# Git
.git/
.gitignore
.gitattributes

# CI/CD
.github/
.gitlab-ci.yml

# Docker
Dockerfile
docker-compose.yml
.dockerignore

# Documentation
README.md
CHANGELOG.md
LICENSE
CODE_OF_CONDUCT.md

# Misc
.husky/
.commitlintrc*
.eslintrc*
.prettierrc*
eslint.config.js
commitlint.config.js
*.md
6 changes: 3 additions & 3 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
MONGO_URI=
JWT_SECRET=
MONGO_URI=mongodb+srv://mukeshdhadhariya1_db_user:sKk4J4Rsmt1MzOEL@cluster0.fc8cebb.mongodb.net/
JWT_SECRET=dsfgh
NODE_ENV=
PORT=mongodb+srv://mukeshdhadhariya1_db_user:sKk4J4Rsmt1MzOEL@cluster0.fc8cebb.mongodb.net/
PORT=3001
171 changes: 171 additions & 0 deletions Docker-commands-guide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
# Docker Commands Guide

This guide provides step-by-step instructions for building images, running containers, stopping them, viewing logs, and managing Docker resources.

---

## 🚀 1. Build Docker Images

Navigate to the folder containing your `Dockerfile` and `docker-compose.yml`.

### Using Docker Compose (Recommended)

```bash
docker-compose build
```

This builds two images:

* `smart-energy-frontend`
* `smart-energy-backend`

---

## 🚀 2. Start Containers

```bash
docker-compose up
```

Run in background:

```bash
docker-compose up -d
```

Access:

* Frontend → [http://localhost:5173](http://localhost:5173)
* Backend → [http://localhost:5000](http://localhost:5000)

---

## 🚀 3. Stop Containers

```bash
docker-compose down
```

This stops and removes containers (not images).

---

## 🚀 4. View Running Containers

```bash
docker ps
```

View all containers (running + stopped):

```bash
docker ps -a
```

---

## 🚀 5. View Images

```bash
docker images
```

---

## 🚀 6. Run a Single Service

Run only frontend:

```bash
docker-compose up frontend
```

Run only backend:

```bash
docker-compose up backend
```

---

## 🚀 7. Build Image Manually (Without Compose)

```bash
docker build -t smart-energy-image .
```

---

## 🚀 8. Run Image Manually

Expose port and run:

```bash
docker run -p 5173:5173 smart-energy-image
```

---

## 🚀 9. Stop a Running Container

Find container ID:

```bash
docker ps
```

Then stop:

```bash
docker stop <container_id>
```

---

## 🚀 10. Remove a Container

```bash
docker rm <container_id>
```

---

## 🚀 11. Remove an Image

```bash
docker rmi <image_id>
```

---

## 🚀 12. Restart Containers

```bash
docker-compose restart
```

---

## 🚀 13. View Logs

Frontend logs:

```bash
docker-compose logs frontend
```

Backend logs:

```bash
docker-compose logs backend
```

Follow logs live:

```bash
docker-compose logs -f
```

---

This `.md` file gives you everything needed to work with Docker images and containers efficiently.
20 changes: 20 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Simple Node.js dev container
FROM node:22-alpine

WORKDIR /app

# Copy only package.json first for caching
COPY package*.json ./

# Install dependencies
RUN npm install

# Copy full project
COPY . .

# Expose BOTH ports (frontend + backend)
EXPOSE 5173
EXPOSE 3001

# Default CMD (overwritten in docker-compose)
CMD ["npm", "run", "dev", "--", "--host", "0.0.0.0"]
58 changes: 58 additions & 0 deletions README.Docker.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Docker Setup for Smart Energy Tracker (Development)

This project uses Docker to run **frontend** and **backend** separately,
each inside its own container, without using nginx or a production build.

---

## Files Included

### 1. Dockerfile
A single minimal Node.js image used for **both frontend and backend**.

- Installs dependencies
- Copies project files
- Exposes ports 5173 (frontend) and 5000 (backend)
- The command is overridden by docker-compose

### 2. docker-compose.yml
Handles running two separate services:

#### `frontend` service
- Runs `npm run dev`
- Exposes port **5173**
- Supports live reload using file volume mounts

#### `backend` service
- Runs `npm start`
- Exposes port **5000**
- Also uses volume mounts for hot reload

### Why Docker Compose?
Because we need:

- Two containers (frontend + backend)
- Each running different commands
- Shared code with hot reload
- Easy `up`, `down`, and log management

---

## How to Run

```bash
docker-compose up --build
```

---

## runing url
- Frontend → http://localhost:5173
- Backend → http://localhost:5000

---

## How to stop
```bash
docker-compose down
```
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,21 @@ Quality checks
- Quick Setup & Run (point-wise commands)


## Environment Setup (.env configuration)

Before running the project, create an environment file.

1. Create a `.env` file in the root directory.
2. Copy the content from `.env.example` and paste it into the `.env` file.

```bash
cp .env.example .env
```

## Demo Login Credentials

Use the following demo account to log in:

- **Email:** `a@gmail.com`
- **Password:** `123`

32 changes: 32 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
version: "3.8"

services:

backend:
build: .
container_name: smart-energy-backend
command: npm start
ports:
- "3001:3001"
environment:
MONGO_URI: "mongodb+srv://mukeshdhadhariya1_db_user:sKk4J4Rsmt1MzOEL@cluster0.fc8cebb.mongodb.net/"
JWT_SECRET: "dsfgh"
NODE_ENV: "development"
PORT: "3001"
volumes:
- .:/app
- /app/node_modules

frontend:
build: .
container_name: smart-energy-frontend
command: npm run dev
ports:
- "5173:5173"
volumes:
- .:/app
- /app/node_modules
environment:
- CHOKIDAR_USEPOLLING=true
depends_on:
- backend
4 changes: 2 additions & 2 deletions src/context/SocketContext.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export function SocketProvider({ children }) {
const isAuthenticated = !!user;

useEffect(() => {

console.log(user)
if (!isAuthenticated) {
if (socket) {
socket.disconnect();
Expand Down Expand Up @@ -130,7 +130,7 @@ export function SocketProvider({ children }) {
setSocket(newSocket);

return () => newSocket.close();
}, [dispatch]);
}, [dispatch,user]);

const value = {
socket,
Expand Down
Loading
Loading