MongoDB Docker and Operations
Introduction
Running mongod in Docker is the fastest way to share a dev database across teammates. Production adds persistent volumes, authentication, replica sets, backups, and upgrade planning. This chapter covers mongod.conf, Compose patterns, mongodump, and operational habits linked to Atlas for managed hosting.
Prerequisites
Single-Node Docker (Development)
Connect:
mongosh "mongodb://root:devrootpass@127.0.0.1:27017/?authSource=admin"Create app user with least privilege:
use hello_demo
db.createUser({
user: "app_user",
pwd: "app_pass",
roles: [{ role: "readWrite", db: "hello_demo" }]
})App URI:
mongodb://app_user:app_pass@127.0.0.1:27017/hello_demo?authSource=hello_demomongod.conf Highlights
Production mongod.conf (conceptual):
| Section | Purpose |
|---|---|
storage.wiredTiger | Cache size—leave RAM headroom for OS |
security.authorization | Require users |
replication.replSetName | Enable replica set (transactions, HA) |
Mount config in Docker:
volumes:
- ./mongod.conf:/etc/mongod.conf
command: ["mongod", "--config", "/etc/mongod.conf"]Three-Node Replica Set (Dev Sketch)
services:
mongo1:
image: mongo:7
command: ["mongod", "--replSet", "rs0", "--bind_ip_all"]
ports: ["27017:27017"]
mongo2:
image: mongo:7
command: ["mongod", "--replSet", "rs0", "--bind_ip_all"]
mongo3:
image: mongo:7
command: ["mongod", "--replSet", "rs0", "--bind_ip_all"]Init once:
rs.initiate({
_id: "rs0",
members: [
{ _id: 0, host: "mongo1:27017" },
{ _id: 1, host: "mongo2:27017" },
{ _id: 2, host: "mongo3:27017" }
]
})Connection string:
mongodb://app_user:pass@mongo1:27017,mongo2:27017,mongo3:27017/hello_demo?replicaSet=rs0Details — Replica Sets.
Resource Limits
services:
mongo:
deploy:
resources:
limits:
cpus: "2"
memory: 4GWithout limits, MongoDB can consume host RAM under load.
Backup With mongodump
mongodump --uri="mongodb://app_user:pass@127.0.0.1:27017/hello_demo" --out=./backup-$(date +%F)Restore:
mongorestore --uri="mongodb://app_user:pass@127.0.0.1:27017" --drop ./backup-2026-05-28/hello_demoWarning
--drop deletes existing data before restore. Test on staging first.
Schedule backups with cron or use Atlas snapshots.
Import / Export
Already covered for data migration — Insert and Import/Export.
Upgrades
Replica set rolling upgrade (concept):
- Upgrade secondaries one at a time
- Step down primary, upgrade former primary
- Verify
db.version()and app smoke tests
Always read release notes for breaking changes.
Logging and Monitoring
| Signal | Tool |
|---|---|
| Slow queries | Profiler, Atlas Performance Advisor — Performance |
| Server metrics | db.serverStatus(), Prometheus exporter |
| Disk | db.stats(), volume alerts |
Full Stack Compose Reference
See Project: MongoDB + Redis + API for api + mongo + redis.
FAQ
Docker MongoDB for production?
Possible with replica set, auth, backups, and monitoring—many teams use Atlas instead.
bindIp 127.0.0.1 breaks remote connect?
Yes—use 0.0.0.0 inside Docker network or proper firewall rules — Troubleshooting.
WiredTiger cache too large?
Set cacheSizeGB below 50% of RAM on dedicated DB hosts.
Sharding in Docker?
Possible for learning; production sharded clusters are ops-heavy — Sharding intro.
systemd on bare metal?
systemctl enable mongod, config in /etc/mongod.conf, logs via journalctl -u mongod.