Add docker-compose for mongodb

This commit is contained in:
2025-08-09 16:29:21 +01:00
parent 37c12046cf
commit 7dc99f847c
2 changed files with 106 additions and 0 deletions

63
DOCKER_SETUP.md Normal file
View File

@@ -0,0 +1,63 @@
# Docker Setup for Recipe Management App
## MongoDB Configuration
This project now includes a Docker Compose setup for MongoDB with persistent storage.
### Environment Variables
Create a `.env` file in the `backend/` directory with the following variables:
```
# MongoDB Configuration (parameterized for security)
MONGODB_USERNAME=admin
MONGODB_PASSWORD=password123
MONGODB_HOST=localhost
MONGODB_PORT=27017
MONGODB_DATABASE=recipe-management
# Alternative: Use full connection string (overrides individual parameters)
# MONGODB_URI=mongodb://admin:password123@localhost:27017/recipe-management?authSource=admin
# Server Configuration
PORT=5000
```
**Note**: The application will automatically construct the MongoDB URI from the individual parameters. You can also override this by setting `MONGODB_URI` directly.
### Starting the Services
1. Start MongoDB and Mongo Express:
```bash
docker-compose up -d
```
2. Start the backend application:
```bash
cd backend
npm start
```
### Services Included
- **MongoDB**: Database server on port 27017 with persistent volume
- **Mongo Express**: Web-based MongoDB admin interface on port 8081
### Default Credentials
- **MongoDB Admin User**: admin
- **MongoDB Admin Password**: password123
- **Database Name**: recipe-management
### Accessing Services
- **Mongo Express**: http://localhost:8081
- **Backend API**: http://localhost:5000
### Data Persistence
MongoDB data is stored in a Docker volume named `mongodb_data` which persists between container restarts.
### Seed Data
The seed data from `backend/seedData.js` is automatically loaded when the MongoDB container starts for the first time.

43
docker-compose.yml Normal file
View File

@@ -0,0 +1,43 @@
version: '3.8'
services:
mongodb:
image: mongo:7.0
container_name: recipe-management-mongodb
restart: unless-stopped
environment:
MONGO_INITDB_ROOT_USERNAME: ${MONGODB_USERNAME}
MONGO_INITDB_ROOT_PASSWORD: ${MONGODB_PASSWORD}
MONGO_INITDB_DATABASE: ${MONGODB_DATABASE}
ports:
- "27017:27017"
volumes:
- mongodb_data:/data/db
- ./backend/seedData.js:/docker-entrypoint-initdb.d/seedData.js:ro
networks:
- recipe-network
# Optional: MongoDB Express for database management
mongo-express:
image: mongo-express:1.0.0
container_name: recipe-management-mongo-express
restart: unless-stopped
ports:
- "8081:8081"
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: ${MONGODB_USERNAME}
ME_CONFIG_MONGODB_ADMINPASSWORD: ${MONGODB_PASSWORD}
ME_CONFIG_MONGODB_URL: mongodb://${MONGODB_USERNAME}:${MONGODB_PASSWORD}@mongodb:27017/
ME_CONFIG_BASICAUTH: false
depends_on:
- mongodb
networks:
- recipe-network
volumes:
mongodb_data:
driver: local
networks:
recipe-network:
driver: bridge