Everything is now containerised and in the docker compose

This commit is contained in:
2025-08-09 17:40:01 +01:00
parent 371f48b42f
commit 22328ae6b1
11 changed files with 375 additions and 51 deletions

View File

@@ -1,63 +1,148 @@
# Docker Setup for Recipe Management App
## MongoDB Configuration
## Full Stack Containerized Setup
This project now includes a Docker Compose setup for MongoDB with persistent storage.
This project includes a complete Docker Compose setup that orchestrates the entire application stack:
- **Frontend**: React TypeScript application with Nginx
- **Backend**: Node.js Express API
- **Database**: MongoDB with persistent storage
- **Admin Interface**: Mongo Express for database management
### Environment Variables
## Quick Start
Create a `.env` file in the `backend/` directory with the following variables:
1. **Create environment file**:
```bash
cp env.example .env
```
Edit the `.env` file with your desired credentials.
```
# MongoDB Configuration (parameterized for security)
2. **Start all services**:
```bash
docker-compose up -d
```
3. **Access the application**:
- **Frontend**: http://localhost:3000
- **Backend API**: http://localhost:5000
- **Mongo Express**: http://localhost:8081
## Environment Configuration
Create a `.env` file in the project root with the following variables:
```bash
# MongoDB Configuration
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
# JWT Secret for Backend Authentication
JWT_SECRET=your-super-secret-jwt-key-change-this-in-production
```
**Note**: The application will automatically construct the MongoDB URI from the individual parameters. You can also override this by setting `MONGODB_URI` directly.
## Services Architecture
### Starting the Services
### Frontend Service
- **Container**: `recipe-management-frontend`
- **Port**: 3000 (mapped to container port 80)
- **Technology**: React TypeScript with Nginx
- **Features**:
- Production-optimized build
- API proxy to backend
- Gzip compression
- Security headers
1. Start MongoDB and Mongo Express:
### Backend Service
- **Container**: `recipe-management-backend`
- **Port**: 5000
- **Technology**: Node.js Express
- **Features**:
- Health checks
- Environment-based configuration
- Automatic MongoDB connection
- Non-root user security
### MongoDB Service
- **Container**: `recipe-management-mongodb`
- **Port**: 27017
- **Features**:
- Persistent data volume
- Health checks
- Automatic seed data loading
- Authentication enabled
### Mongo Express Service
- **Container**: `recipe-management-mongo-express`
- **Port**: 8081
- **Features**:
- Web-based MongoDB administration
- Connected to main MongoDB instance
## Development Workflow
### Building and Running
```bash
docker-compose up -d
# Build and start all services
docker-compose up -d --build
# View logs
docker-compose logs -f
# Stop all services
docker-compose down
# Stop and remove volumes (WARNING: This deletes data)
docker-compose down -v
```
2. Start the backend application:
### Individual Service Management
```bash
cd backend
npm start
# Restart specific service
docker-compose restart backend
# View logs for specific service
docker-compose logs -f frontend
# Execute commands in running container
docker-compose exec backend sh
```
### Services Included
## Data Persistence
- **MongoDB**: Database server on port 27017 with persistent volume
- **Mongo Express**: Web-based MongoDB admin interface on port 8081
- **MongoDB Data**: Stored in `mongodb_data` Docker volume
- **Seed Data**: Automatically loaded from `backend/seedData.js` on first startup
- **Persistent Storage**: Data survives container restarts and rebuilds
### Default Credentials
## Network Architecture
- **MongoDB Admin User**: admin
- **MongoDB Admin Password**: password123
- **Database Name**: recipe-management
All services communicate through a dedicated Docker network (`recipe-network`):
- Frontend → Backend: Internal container communication
- Backend → MongoDB: Internal container communication
- External access via mapped ports only
### Accessing Services
## Security Features
- **Mongo Express**: http://localhost:8081
- **Backend API**: http://localhost:5000
- **Non-root containers**: All services run as non-privileged users
- **Environment-based secrets**: Credentials managed via environment variables
- **Network isolation**: Services communicate only through defined network
- **Health checks**: Automatic service health monitoring
- **Security headers**: Frontend served with security headers via Nginx
### Data Persistence
## Troubleshooting
MongoDB data is stored in a Docker volume named `mongodb_data` which persists between container restarts.
### Common Issues
1. **Port conflicts**: Ensure ports 3000, 5000, 8081, 27017 are available
2. **Environment variables**: Check `.env` file exists and has correct values
3. **Build failures**: Run `docker-compose build --no-cache` to rebuild from scratch
### Seed Data
### Useful Commands
```bash
# Check service status
docker-compose ps
The seed data from `backend/seedData.js` is automatically loaded when the MongoDB container starts for the first time.
# View resource usage
docker stats
# Clean up unused Docker resources
docker system prune -f
```