docker.recipes

Express + MongoDB

beginner

Classic Node.js Express server with MongoDB and Mongoose.

Overview

Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications. Created by TJ Holowaydchuk in 2010, Express has become the de facto standard for Node.js web development, powering millions of applications with its unopinionated, middleware-based architecture. It simplifies HTTP server creation while maintaining the flexibility to build everything from simple APIs to complex web applications. This stack combines Express with MongoDB through Mongoose ODM, creating a powerful JavaScript-based development environment where both the application logic and database operations use similar syntax and paradigms. MongoDB's document-based storage naturally complements Express's JSON-centric request/response cycle, eliminating the object-relational impedance mismatch common in traditional SQL-based stacks. The flexible schema design of MongoDB allows rapid iteration during development, while Express's middleware system provides clean separation of concerns for authentication, validation, and business logic. This combination is ideal for JavaScript developers who want to maintain a single language across their entire stack, startups requiring rapid prototyping capabilities, and applications dealing with evolving data structures. The stack excels in scenarios where development speed is crucial and data relationships are not overly complex, making it particularly valuable for content management systems, social platforms, and API-first applications where JSON document storage aligns naturally with frontend requirements.

Key Features

  • Mongoose ODM integration providing schema validation, middleware hooks, and query building for MongoDB operations
  • Express middleware ecosystem with thousands of plugins for authentication, logging, parsing, and security
  • JSON-first data flow from MongoDB documents through Express routes to client applications
  • MongoDB aggregation pipeline support for complex data transformations and analytics within Express routes
  • Automatic Express route parameter validation using express-validator with MongoDB ObjectId support
  • Hot-reload development capabilities using nodemon for rapid Express application development
  • MongoDB connection pooling and automatic reconnection handling through Mongoose
  • Express static file serving combined with GridFS for MongoDB-based file storage solutions

Common Use Cases

  • 1RESTful API development for mobile applications requiring flexible data schemas and rapid iteration
  • 2Content management systems where article, media, and user data benefit from MongoDB's document flexibility
  • 3Real-time chat applications leveraging MongoDB change streams with Express WebSocket integration
  • 4E-commerce platforms needing product catalogs with varying attributes and Express checkout workflows
  • 5Social media platforms storing user profiles, posts, and activity feeds in MongoDB documents
  • 6IoT data collection systems where Express endpoints receive sensor data stored in MongoDB time-series collections
  • 7Rapid MVP development for startups requiring quick database schema changes without migrations

Prerequisites

  • Docker and Docker Compose installed with at least 2GB available RAM for MongoDB operations
  • Node.js application code with package.json defining Express and Mongoose dependencies
  • Basic understanding of JavaScript promises/async-await for MongoDB connection handling
  • Port 3000 available for Express server and understanding of MongoDB URI connection strings
  • Familiarity with Mongoose schema definition and Express routing patterns
  • Environment variable configuration knowledge for database connection parameters

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 express:
3 build: .
4 container_name: express
5 environment:
6 MONGODB_URI: mongodb://mongodb:27017/${DB_NAME}
7 ports:
8 - "3000:3000"
9 depends_on:
10 - mongodb
11 networks:
12 - express-network
13
14 mongodb:
15 image: mongo:7
16 volumes:
17 - mongo_data:/data/db
18 networks:
19 - express-network
20
21volumes:
22 mongo_data:
23
24networks:
25 express-network:
26 driver: bridge

.env Template

.env
1DB_NAME=express

Usage Notes

  1. 1Docs: https://expressjs.com/
  2. 2Access at http://localhost:3000
  3. 3Mongoose for MongoDB ODM with schemas
  4. 4Minimal and unopinionated
  5. 5Add middleware: cors, helmet, morgan for logging
  6. 6Use express-validator for input validation

Individual Services(2 services)

Copy individual services to mix and match with your existing compose files.

express
express:
  build: .
  container_name: express
  environment:
    MONGODB_URI: mongodb://mongodb:27017/${DB_NAME}
  ports:
    - "3000:3000"
  depends_on:
    - mongodb
  networks:
    - express-network
mongodb
mongodb:
  image: mongo:7
  volumes:
    - mongo_data:/data/db
  networks:
    - express-network

Quick Start

terminal
1# 1. Create the compose file
2cat > docker-compose.yml << 'EOF'
3services:
4 express:
5 build: .
6 container_name: express
7 environment:
8 MONGODB_URI: mongodb://mongodb:27017/${DB_NAME}
9 ports:
10 - "3000:3000"
11 depends_on:
12 - mongodb
13 networks:
14 - express-network
15
16 mongodb:
17 image: mongo:7
18 volumes:
19 - mongo_data:/data/db
20 networks:
21 - express-network
22
23volumes:
24 mongo_data:
25
26networks:
27 express-network:
28 driver: bridge
29EOF
30
31# 2. Create the .env file
32cat > .env << 'EOF'
33DB_NAME=express
34EOF
35
36# 3. Start the services
37docker compose up -d
38
39# 4. View logs
40docker 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/express-mongodb/run | bash

Troubleshooting

  • MongoServerError - Authentication failed: Ensure MONGODB_URI doesn't include credentials or MongoDB container isn't configured with auth
  • Express server exits with code 0: Check that app.listen() is called and Express isn't terminating due to unhandled MongoDB connection errors
  • Cannot connect to MongoDB at startup: Add retry logic in Express app or use depends_on with healthcheck conditions for MongoDB container
  • Mongoose connection timeout errors: Increase MongoDB container startup time or implement connection retry with exponential backoff
  • Express routes returning 404 for API calls: Verify Express router mounting and ensure MongoDB connection is established before route registration
  • High memory usage in MongoDB container: Adjust WiredTiger cache size or increase container memory limits beyond default 512MB

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