Skip to content

Commit

Permalink
added docker configurations (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
mani1soni authored Jan 20, 2024
1 parent df9e454 commit cd15768
Show file tree
Hide file tree
Showing 6 changed files with 183 additions and 1 deletion.
26 changes: 26 additions & 0 deletions Dockerfile-backend
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Dockerfile-backend

# Use Node.js as the base image
FROM node:20

# Set the working directory
WORKDIR /app

# Copy package.json and package-lock.json
COPY server/package*.json ./

# Install dependencies
RUN npm install

# Copy the server directory and the startup script
COPY server/ ./
COPY startup.sh ./

# Make the script executable
RUN chmod +x startup.sh

# Expose the backend port
EXPOSE 5002

# Command to run the startup script
CMD ["./startup.sh"]
22 changes: 22 additions & 0 deletions Dockerfile-frontend
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Dockerfile-frontend

# Use Node.js as the base image
FROM node:20

# Set the working directory
WORKDIR /app

# Copy package.json and package-lock.json
COPY client/package*.json ./

# Install dependencies
RUN npm install

# Copy the client directory
COPY client/ .

# Expose the frontend port
EXPOSE 5173

# Command to run the app
CMD ["npm", "run", "dev"]
2 changes: 1 addition & 1 deletion client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"dev": "vite --host",
"build": "vite build",
"lint": "eslint . --ext js,jsx --report-unused-disable-directives --max-warnings 0",
"preview": "vite preview"
Expand Down
94 changes: 94 additions & 0 deletions docker-compose.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# Running the MealMatters Application with Docker Compose

This guide will help you set up and run the MealMatters application using Docker and Docker Compose. It includes instructions for installing Docker on various operating systems, setting up the project, and running the application.

## Installing Docker

### Linux

1. **Update your package index:**
```bash
sudo apt-get update
```
2. **Install Docker:**
```bash
sudo apt-get install docker-ce docker-ce-cli containerd.io
```
3. **Verify the installation:**
```bash
sudo docker run hello-world
```

For detailed instructions, visit the [Docker installation guide for Linux](https://docs.docker.com/engine/install/ubuntu/).

### Windows

1. **Download Docker Desktop for Windows** from the [official Docker website](https://docs.docker.com/docker-for-windows/install/).
2. **Run the installer** and follow the instructions.
3. **Start Docker Desktop** from your applications or programs menu.

### macOS

1. **Download Docker Desktop for Mac** from the [official Docker website](https://docs.docker.com/docker-for-mac/install/).
2. **Open the downloaded file** and drag the Docker icon to your Applications folder.
3. **Start Docker Desktop** from your Applications folder.

### Docker Compose

Docker Compose should be automatically installed with Docker Desktop for Windows and macOS. For Linux, follow the instructions at [Install Docker Compose](https://docs.docker.com/compose/install/).

## Project Structure

Ensure your project directory is structured as follows:

```
MealMatters/
├── client/ # Frontend source files
│ ├── (frontend files)
│ └── package.json
├── server/ # Backend source files
│ ├── (backend files)
│ └── package.json
├── Dockerfile-frontend # Dockerfile for the frontend
├── Dockerfile-backend # Dockerfile for the backend
├── docker-compose.yml # Docker Compose file
└── startup.sh # Startup script for the backend
```

## Dockerfiles and Docker Compose File

Set up `Dockerfile-frontend`, `Dockerfile-backend`, `docker-compose.yml`, and `startup.sh` as previously outlined.

## Running the Application

1. **Build and Run with Docker Compose:**
In your project's root directory, run:

```bash
docker-compose up --build
```
2. **Accessing the Application:**
- The frontend is available at `http://localhost:5173`.
- The backend API is at `http://localhost:5000`.

3. **Stopping the Application:**
To stop, press `Ctrl+C`. To remove containers, run:

```bash
docker-compose down
```

## Additional Notes

- **JWT Secret:** Generated dynamically by `startup.sh` on each container start. Previous JWT tokens become invalid after each restart

.
- **Data Persistence:** MongoDB data persists in a Docker volume as defined in `docker-compose.yml`.
- **Security:** Handle sensitive information carefully, especially in production.

## Conclusion

This guide provides a comprehensive overview of setting up and running the MealMatters application using Docker and Docker Compose. For more information or troubleshooting, refer to the [Docker documentation](https://docs.docker.com/).
33 changes: 33 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
version: '3.8'
services:
client:
build:
context: .
dockerfile: Dockerfile-frontend
ports:
- "5173:5173"
depends_on:
- server

server:
build:
context: .
dockerfile: Dockerfile-backend
ports:
- "5000:5000"
environment:
- MONGO_URL=mongodb://mongo:27017/mealMatters
- PORT=5000
depends_on:
- mongo

mongo:
image: mongo:latest
environment:
- MONGO_INITDB_ROOT_USERNAME=mongoadmin
- MONGO_INITDB_ROOT_PASSWORD=secret
volumes:
- mongodb_data:/data/db

volumes:
mongodb_data:
7 changes: 7 additions & 0 deletions startup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash

# Generate a new JWT secret
export JWT_SECRET=$(openssl rand -base64 32)

# Start the Node.js application
npm start

0 comments on commit cd15768

Please sign in to comment.