MERN Stack
A full-featured MERN (MongoDB, Express, React, Node.js) development and production stack. MongoDB 7 runs with authentication enabled and a dedicated replica-set-ready configuration for change streams. The Node 22 backend uses volume mounts and nodemon for hot reload during development, while the React frontend runs its own dev server. Nginx acts as a unified reverse proxy routing /api requests to the Express backend and all other traffic to the React dev server. Network separation isolates the database on the backend network while only Nginx is publicly exposed.
Included Services
nginx
nginx:1.27-alpine
frontend
node:22-alpine
Environment Variables:
backend
node:22-alpine
Environment Variables:
mongodb
mongo:7
Environment Variables:
Generated YAML
# Generated by ComposeHub (composehub.dev)
name: mern-stack
services:
nginx:
image: nginx:1.27-alpine
restart: unless-stopped
ports:
- 80:80
volumes:
- nginx_conf:/etc/nginx/conf.d
networks:
- frontend
depends_on:
backend:
condition: service_healthy
frontend:
condition: service_started
healthcheck:
test:
- CMD
- curl
- "-f"
- http://localhost
interval: 30s
timeout: 10s
retries: 3
start_period: 10s
deploy:
resources:
limits:
cpus: "0.50"
memory: 256M
reservations:
cpus: "0.10"
memory: 64M
labels:
com.composeforge.description: Nginx reverse proxy routing /api to backend and / to frontend
frontend:
image: node:22-alpine
restart: unless-stopped
command:
- sh
- "-c"
- npm install && npm run dev
working_dir: /app
expose:
- "5173"
volumes:
- frontend_src:/app
- frontend_node_modules:/app/node_modules
environment:
NODE_ENV: ${NODE_ENV:-development}
VITE_API_URL: ${VITE_API_URL:-/api}
networks:
- frontend
deploy:
resources:
limits:
cpus: "1.00"
memory: 512M
reservations:
cpus: "0.25"
memory: 128M
labels:
com.composeforge.description: React development server (Vite)
backend:
image: node:22-alpine
restart: unless-stopped
command:
- sh
- "-c"
- npm install && npx nodemon src/index.js
working_dir: /app
expose:
- "3000"
volumes:
- backend_src:/app
- backend_node_modules:/app/node_modules
environment:
NODE_ENV: ${NODE_ENV:-development}
PORT: ${BACKEND_PORT:-3000}
MONGODB_URI: mongodb://${MONGO_USER:-app}:${MONGO_PASSWORD:-changeme}@mongodb:27017/${MONGO_DB:-mernapp}?authSource=admin
JWT_SECRET: ${JWT_SECRET:-change-this-secret}
networks:
- frontend
- backend
depends_on:
mongodb:
condition: service_healthy
healthcheck:
test:
- CMD-SHELL
- wget --no-verbose --tries=1 --spider http://localhost:3000/health || exit 1
interval: 30s
timeout: 10s
retries: 5
start_period: 30s
deploy:
resources:
limits:
cpus: "1.00"
memory: 512M
reservations:
cpus: "0.25"
memory: 128M
labels:
com.composeforge.description: Express.js API backend with hot reload
mongodb:
image: mongo:7
restart: unless-stopped
volumes:
- mongodb_data:/data/db
- mongodb_config:/data/configdb
environment:
MONGO_INITDB_ROOT_USERNAME: ${MONGO_USER:-app}
MONGO_INITDB_ROOT_PASSWORD: ${MONGO_PASSWORD:-changeme}
MONGO_INITDB_DATABASE: ${MONGO_DB:-mernapp}
networks:
- backend
healthcheck:
test:
- CMD
- mongosh
- "--eval"
- db.adminCommand('ping')
- "--quiet"
interval: 30s
timeout: 10s
retries: 5
start_period: 40s
deploy:
resources:
limits:
cpus: "1.00"
memory: 512M
reservations:
cpus: "0.25"
memory: 256M
labels:
com.composeforge.description: MongoDB 7 document database with authentication
networks:
frontend:
driver: bridge
backend:
driver: bridge
volumes:
nginx_conf:
driver: local
frontend_src:
driver: local
frontend_node_modules:
driver: local
backend_src:
driver: local
backend_node_modules:
driver: local
mongodb_data:
driver: local
mongodb_config:
driver: local
Quick Info
- Services
- 4
- Networks
- 2
- Volumes
- 7
When to Use
This template is ideal for setting up a mern stack environment. All services are pre-configured with healthchecks, resource limits, and sensible defaults. Customize environment variables before deploying to production.
Tips
- Change all default passwords before deploying
- Review resource limits for your hardware
- Add a reverse proxy for production HTTPS
- Configure backup strategies for data volumes