MongoDB is a powerful NoSQL database widely used in modern applications. When building full-stack apps locally, it’s helpful to pair MongoDB with a lightweight admin interface, and Mongo Express is the perfect fit.
In this guide, you’ll learn how to spin up MongoDB with Mongo Express using Docker Compose. No native installs, no config nightmares — just containers working together like magic.
✅ Prerequisites
To follow along, make sure you have:
- Docker
- Docker Compose
This setup works on Windows, macOS, and Linux.
🛠 Step-by-Step Setup Guide
📁 Step 1: Create your project directory
Open your terminal and run:
mkdir mongo-docker-setup
cd mongo-docker-setup
📝 Step 2: Create the docker-compose.yml
file
Inside the folder, create a file named docker-compose.yml
and paste this content:
services:
# MongoDB
mongodb:
image: mongodb/mongodb-community-server:latest
container_name: mongodb
ports:
- "27017:27017"
volumes:
- mongodb_data:/data/db
environment:
- MONGO_INITDB_ROOT_USERNAME=user
- MONGO_INITDB_ROOT_PASSWORD=root
networks:
- mongodb_network
healthcheck:
test:
- CMD
- mongosh
- 'mongodb://localhost:27017/admin'
- '--eval=db.runCommand({ ping: 1 })'
retries: 3
timeout: 5s
# Mongo Express
mongo-express:
image: mongo-express:latest
container_name: mongo-express
restart: unless-stopped
ports:
- "8081:8081"
environment:
ME_CONFIG_MONGODB_SERVER: mongodb
ME_CONFIG_MONGODB_ADMINUSERNAME: user
ME_CONFIG_MONGODB_ADMINPASSWORD: root
ME_CONFIG_MONGODB_ENABLE_ADMIN: "true"
depends_on:
- mongodb
networks:
- mongodb_network
networks:
mongodb_network:
driver: bridge
volumes:
mongodb_data:
driver: local
🔍 What This Does
- Sets up a MongoDB server with a root username/password.
- Stores data persistently using a Docker volume.
- Launches Mongo Express as a web-based GUI for your database.
- Connects the two services over a Docker bridge network.
- Exposes ports
27017
for MongoDB and8081
for Mongo Express.
🚀 Step 3: Start the containers
Run the following command:
docker compose up -d
This will pull the necessary images and start both services in the background.
✅ Step 4: Access Mongo Express
Once the services are up, visit:
http://localhost:8081
(If you see not found page, then run the following command in the terminal)
docker exec -it mongodb mongosh --username user --password root --authenticationDatabase admin
- Credential for Admin Login: admin/pass
You’ll see the Mongo Express dashboard, where you can:
- Browse databases
- Create collections
- Add, edit, or delete documents
- Run queries using MongoDB syntax
Login is automatic because the credentials are already passed through environment variables.
🧼 Step 5: Stop and clean up (optional)
To stop and remove everything:
docker compose down
This stops the services and removes the volumes (i.e., your MongoDB data will be wiped).
🔚 Final Thoughts
Github Link: https://github.com/rafi021/mongodb-mongo-express-docker-compose
Youtube: https://youtu.be/Dj-tQg_srxM
With just a few lines of Docker Compose, you’ve created a full MongoDB development environment — complete with a UI for browsing and managing data. No need to install MongoDB locally or mess with GUIs. Just run, develop, and enjoy!
If you found this guide useful, consider sharing it with your team. Want to see similar Docker setups for Redis, PostgreSQL, RabbitMQ, or Elasticsearch? Let me know in the comments!
Top comments (0)