MERN Stack
MongoDB, Express, React, Node.js full-stack JavaScript setup.
Overview
MongoDB is a NoSQL document database that revolutionized web development by storing data in flexible, JSON-like documents rather than rigid table structures. Launched in 2009, MongoDB became the foundation for modern JavaScript applications by providing schema flexibility, horizontal scaling capabilities, and native JSON support that aligns perfectly with JavaScript's object-oriented nature.
The MERN stack combines MongoDB's document storage with Express.js (Node.js web framework), React (frontend library), and Node.js runtime to create a full-JavaScript development environment. This configuration eliminates context switching between different programming languages and data formats, as JavaScript objects flow naturally from React components through Express APIs to MongoDB documents. The stack leverages MongoDB's aggregation framework for complex queries, Express middleware for API logic, React's component architecture for dynamic UIs, and Node.js's event-driven model for handling concurrent requests.
This stack is ideal for startups building MVP applications, development teams wanting rapid prototyping capabilities, and organizations creating content-heavy applications with evolving data requirements. The MERN combination excels in scenarios requiring real-time features, user-generated content, and applications where data structures frequently change during development cycles.
Key Features
- Flexible document schema allowing data structure evolution without migrations
- MongoDB aggregation framework for complex data analytics and reporting
- React hot-reloading for instant UI updates during development
- Express.js middleware ecosystem with thousands of available plugins
- Node.js non-blocking I/O for handling thousands of concurrent connections
- MongoDB change streams for real-time data synchronization across components
- Single JavaScript language across entire application stack
- MongoDB horizontal sharding for scaling beyond single server limitations
Common Use Cases
- 1Social media platforms requiring user profiles, posts, and real-time interactions
- 2E-commerce applications with product catalogs, user reviews, and shopping carts
- 3Content management systems with dynamic article structures and media handling
- 4Real-time collaboration tools like project management or document editing platforms
- 5IoT dashboards collecting and visualizing sensor data with flexible schemas
- 6Startup MVPs requiring rapid feature development and frequent data model changes
- 7Educational platforms with courses, user progress tracking, and interactive content
Prerequisites
- Docker Engine 20.10+ and Docker Compose V2 installed on host system
- Minimum 4GB RAM available (MongoDB requires 2GB+, Node.js applications need 512MB each)
- Ports 3000, 5000, and 27017 available on host machine
- Basic JavaScript/ES6 knowledge for customizing React and Express components
- Understanding of RESTful API design principles for Express.js backend development
- Familiarity with MongoDB query syntax and document-oriented data modeling
For development & testing. Review security settings, change default credentials, and test thoroughly before production use. See Terms
docker-compose.yml
docker-compose.yml
1services: 2 frontend: 3 build: 4 context: ./frontend5 dockerfile: Dockerfile6 container_name: frontend7 ports: 8 - "3000:3000"9 networks: 10 - mern1112 backend: 13 build: 14 context: ./backend15 dockerfile: Dockerfile16 container_name: backend17 environment: 18 MONGODB_URI: mongodb://mongodb:27017/${DB_NAME}19 ports: 20 - "5000:5000"21 depends_on: 22 - mongodb23 networks: 24 - mern2526 mongodb: 27 image: mongo:728 container_name: mongodb29 volumes: 30 - mongodb_data:/data/db31 networks: 32 - mern3334volumes: 35 mongodb_data: 3637networks: 38 mern: 39 driver: bridge.env Template
.env
1DB_NAME=mernappUsage Notes
- 1Docs: https://www.mongodb.com/mern-stack
- 2Create frontend/ with Create React App or Vite
- 3Create backend/ with Express.js API
- 4Frontend at http://localhost:3000, API at http://localhost:5000
- 5MongoDB connection: mongodb://mongodb:27017/dbname
- 6Add hot-reload with volume mounts in development
Individual Services(3 services)
Copy individual services to mix and match with your existing compose files.
frontend
frontend:
build:
context: ./frontend
dockerfile: Dockerfile
container_name: frontend
ports:
- "3000:3000"
networks:
- mern
backend
backend:
build:
context: ./backend
dockerfile: Dockerfile
container_name: backend
environment:
MONGODB_URI: mongodb://mongodb:27017/${DB_NAME}
ports:
- "5000:5000"
depends_on:
- mongodb
networks:
- mern
mongodb
mongodb:
image: mongo:7
container_name: mongodb
volumes:
- mongodb_data:/data/db
networks:
- mern
Quick Start
terminal
1# 1. Create the compose file2cat > docker-compose.yml << 'EOF'3services:4 frontend:5 build:6 context: ./frontend7 dockerfile: Dockerfile8 container_name: frontend9 ports:10 - "3000:3000"11 networks:12 - mern1314 backend:15 build:16 context: ./backend17 dockerfile: Dockerfile18 container_name: backend19 environment:20 MONGODB_URI: mongodb://mongodb:27017/${DB_NAME}21 ports:22 - "5000:5000"23 depends_on:24 - mongodb25 networks:26 - mern2728 mongodb:29 image: mongo:730 container_name: mongodb31 volumes:32 - mongodb_data:/data/db33 networks:34 - mern3536volumes:37 mongodb_data:3839networks:40 mern:41 driver: bridge42EOF4344# 2. Create the .env file45cat > .env << 'EOF'46DB_NAME=mernapp47EOF4849# 3. Start the services50docker compose up -d5152# 4. View logs53docker compose logs -fOne-Liner
Run this command to download and set up the recipe in one step:
terminal
1curl -fsSL https://docker.recipes/api/recipes/mern-stack/run | bashTroubleshooting
- MongoNetworkError connection refused: Ensure MongoDB container is running and backend depends_on is properly configured
- React app shows blank page: Check browser console for JavaScript errors and verify frontend build completed successfully
- CORS errors when calling API: Add cors middleware to Express.js backend or configure proxy in React development server
- MongoDB container exits with code 125: Increase Docker Desktop memory allocation to at least 4GB for MongoDB operations
- Hot reload not working in development: Add volume mounts for ./frontend/src and ./backend directories to docker-compose.yml
- Express API returns 404 for all routes: Verify Express router setup and ensure routes are properly mounted in app.js
Community Notes
Loading...
Loading notes...
Download Recipe Kit
Get all files in a ready-to-deploy package
Includes docker-compose.yml, .env template, README, and license
Ad Space
Shortcuts: C CopyF FavoriteD Download