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:

bash
mongosh "mongodb://root:devrootpass@127.0.0.1:27017/?authSource=admin"

Create app user with least privilege:

javascript
use hello_demo
db.createUser({
  user: "app_user",
  pwd: "app_pass",
  roles: [{ role: "readWrite", db: "hello_demo" }]
})

App URI:

text
mongodb://app_user:app_pass@127.0.0.1:27017/hello_demo?authSource=hello_demo

mongod.conf Highlights

Production mongod.conf (conceptual):

SectionPurpose
storage.wiredTigerCache size—leave RAM headroom for OS
security.authorizationRequire users
replication.replSetNameEnable replica set (transactions, HA)

Mount config in Docker:

yaml
volumes:
  - ./mongod.conf:/etc/mongod.conf
command: ["mongod", "--config", "/etc/mongod.conf"]

Three-Node Replica Set (Dev Sketch)

yaml
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:

javascript
rs.initiate({
  _id: "rs0",
  members: [
    { _id: 0, host: "mongo1:27017" },
    { _id: 1, host: "mongo2:27017" },
    { _id: 2, host: "mongo3:27017" }
  ]
})

Connection string:

text
mongodb://app_user:pass@mongo1:27017,mongo2:27017,mongo3:27017/hello_demo?replicaSet=rs0

Details — Replica Sets.

Resource Limits

yaml
services:
  mongo:
    deploy:
      resources:
        limits:
          cpus: "2"
          memory: 4G

Without limits, MongoDB can consume host RAM under load.

Backup With mongodump

bash
mongodump --uri="mongodb://app_user:pass@127.0.0.1:27017/hello_demo" --out=./backup-$(date +%F)

Restore:

bash
mongorestore --uri="mongodb://app_user:pass@127.0.0.1:27017" --drop ./backup-2026-05-28/hello_demo

Warning

--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):

  1. Upgrade secondaries one at a time
  2. Step down primary, upgrade former primary
  3. Verify db.version() and app smoke tests

Always read release notes for breaking changes.

Logging and Monitoring

SignalTool
Slow queriesProfiler, Atlas Performance Advisor — Performance
Server metricsdb.serverStatus(), Prometheus exporter
Diskdb.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.