docker.recipes

MERN Stack

intermediate

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: ./frontend
5 dockerfile: Dockerfile
6 container_name: frontend
7 ports:
8 - "3000:3000"
9 networks:
10 - mern
11
12 backend:
13 build:
14 context: ./backend
15 dockerfile: Dockerfile
16 container_name: backend
17 environment:
18 MONGODB_URI: mongodb://mongodb:27017/${DB_NAME}
19 ports:
20 - "5000:5000"
21 depends_on:
22 - mongodb
23 networks:
24 - mern
25
26 mongodb:
27 image: mongo:7
28 container_name: mongodb
29 volumes:
30 - mongodb_data:/data/db
31 networks:
32 - mern
33
34volumes:
35 mongodb_data:
36
37networks:
38 mern:
39 driver: bridge

.env Template

.env
1DB_NAME=mernapp

Usage Notes

  1. 1Docs: https://www.mongodb.com/mern-stack
  2. 2Create frontend/ with Create React App or Vite
  3. 3Create backend/ with Express.js API
  4. 4Frontend at http://localhost:3000, API at http://localhost:5000
  5. 5MongoDB connection: mongodb://mongodb:27017/dbname
  6. 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 file
2cat > docker-compose.yml << 'EOF'
3services:
4 frontend:
5 build:
6 context: ./frontend
7 dockerfile: Dockerfile
8 container_name: frontend
9 ports:
10 - "3000:3000"
11 networks:
12 - mern
13
14 backend:
15 build:
16 context: ./backend
17 dockerfile: Dockerfile
18 container_name: backend
19 environment:
20 MONGODB_URI: mongodb://mongodb:27017/${DB_NAME}
21 ports:
22 - "5000:5000"
23 depends_on:
24 - mongodb
25 networks:
26 - mern
27
28 mongodb:
29 image: mongo:7
30 container_name: mongodb
31 volumes:
32 - mongodb_data:/data/db
33 networks:
34 - mern
35
36volumes:
37 mongodb_data:
38
39networks:
40 mern:
41 driver: bridge
42EOF
43
44# 2. Create the .env file
45cat > .env << 'EOF'
46DB_NAME=mernapp
47EOF
48
49# 3. Start the services
50docker compose up -d
51
52# 4. View logs
53docker compose logs -f

One-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 | bash

Troubleshooting

  • 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