Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/dokploy/dokploy/llms.txt

Use this file to discover all available pages before exploring further.

MongoDB is a document-oriented NoSQL database known for flexibility, scalability, and developer-friendly features. It stores data in JSON-like documents with dynamic schemas.

Creating a MongoDB Database

1

Navigate to Databases

In your Dokploy dashboard, select your environment and click Add Database > MongoDB.
2

Configure Basic Settings

Provide the basic information:
  • Name: Display name for your database
  • App Name: Internal DNS name (e.g., mongo-prod)
  • Description: Optional notes
  • Docker Image: MongoDB version (default: mongo:15)
3

Set Credentials

MongoDB requires authentication credentials:
Database User: admin
Database Password: <strong-password>
Passwords cannot contain: $ ! ' " \ / or spaces
MongoDB creates a user with root privileges. You can create additional users after deployment.
4

Optional: Enable Replica Sets

For production deployments, enable replica sets:
  • Check Enable Replica Sets
  • Provides high availability and data redundancy
  • Required for transactions and change streams
Replica sets require additional configuration after deployment.
5

Deploy

Click Create to deploy. Dokploy will:
  • Pull the MongoDB Docker image
  • Create a volume at /data/db
  • Initialize with your credentials

Connection Information

Applications in the same environment use internal DNS:
Host: <app-name>  # e.g., mongo-prod
Port: 27017
Username: admin
Password: <your-password>
Auth Database: admin
Connection String Examples:
# Standard format
mongodb://admin:password@mongo-prod:27017

# With authentication database
mongodb://admin:password@mongo-prod:27017/?authSource=admin

# With specific database
mongodb://admin:password@mongo-prod:27017/myapp?authSource=admin

# MongoDB Shell
mongosh mongodb://admin:password@mongo-prod:27017/myapp?authSource=admin

Configuration

Environment Variables

Customize MongoDB with environment variables:
# Performance settings
MONGO_MAXPOOLSIZE=100
MONGO_CONNECTTIMEOUTMS=10000
MONGO_SOCKETTIMEOUTMS=30000

# WiredTiger storage engine
MONGO_WIREDTIGER_CACHE_SIZE_GB=1
MONGO_WIREDTIGER_CONCURRENT_READ_TRANSACTIONS=128
MONGO_WIREDTIGER_CONCURRENT_WRITE_TRANSACTIONS=128

# Logging
MONGO_VERBOSE=v
MONGO_LOGPATH=/var/log/mongodb/mongod.log

# Security
MONGO_AUTH=true
MONGO_BIND_IP_ALL=false

# Replication (if replica sets enabled)
MONGO_REPLSET=rs0

# Timezone
TZ=America/New_York

Resource Allocation

Recommended resources by workload:
WorkloadMemory ReservationMemory LimitCPU ReservationCPU Limit
Development256MB512MB0.10.25
Small Application512MB1GB0.250.5
Production1GB2GB0.51.0
High Performance2GB8GB1.04.0
MongoDB benefits from more memory for caching. Allocate at least 1GB for production workloads.

Docker Image Versions

MongoDB version options:
# Community Edition
mongo:15           # Default version
mongo:7            # MongoDB 7.x
mongo:6            # MongoDB 6.x
mongo:5            # MongoDB 5.x

# Specific versions
mongo:7.0.5
mongo:6.0.13

# Note: Version 15 in the default is likely mongo:5 or mongo:6
# Verify available versions at hub.docker.com/_/mongo

Database Operations

Lifecycle Management

Control database availability:Start:
  • Status: done
  • Accepts connections
  • Loads data from volume
Stop:
  • Status: idle
  • Closes connections gracefully
  • Data persists in volume
  • No resource usage

Volume Management

MongoDB stores data in a Docker volume:
Volume Name: {appName}-data
Mount Path: /data/db
Volume contents:
  • Database files (WiredTiger)
  • Collection data
  • Indexes
  • Journal files
  • Configuration

Backup Configuration

See Database Backups for automated backup setup using mongodump.

Advanced Configuration

Custom Startup Options

Command:
mongod
Arguments:
--wiredTigerCacheSizeGB 1
--maxConns 100
--bind_ip_all
--auth
--logpath /var/log/mongodb/mongod.log
--logappend

Replica Set Configuration

If you enabled replica sets during creation:
  1. Connect to MongoDB:
    mongosh mongodb://admin:password@mongo-prod:27017?authSource=admin
    
  2. Initialize replica set:
    rs.initiate({
      _id: "rs0",
      members: [
        { _id: 0, host: "mongo-prod:27017" }
      ]
    })
    
  3. Verify status:
    rs.status()
    

Health Check Configuration

{
  "test": ["CMD-SHELL", "mongosh --eval 'db.adminCommand({\"ping\": 1})'"],
  "interval": 30000000000,
  "timeout": 5000000000,
  "retries": 3
}

Restart Policy

{
  "condition": "on-failure",
  "delay": 5000000000,
  "maxAttempts": 3
}

Common Use Cases

Standard MongoDB for web apps:
Docker Image: mongo:7
Memory Limit: 1GB
CPU Limit: 0.5
Replica Sets: Disabled

# Environment
MONGO_MAXPOOLSIZE=50
MONGO_WIREDTIGER_CACHE_SIZE_GB=0.5

Monitoring and Logs

View MongoDB logs:
  1. Open your MongoDB service
  2. Go to Logs tab
  3. Monitor database activity
Common log patterns:
# Successful startup
Waiting for connections on port 27017

# Connection accepted
Connection accepted from 172.x.x.x:xxxxx

# Slow query
Slow query: 1234ms { find: "users", filter: { ... } }

# Replica set status
Replica set state transition: SECONDARY -> PRIMARY

Troubleshooting

Common authentication issues:Symptoms:
  • Authentication failed errors
  • Cannot connect with credentials
Solutions:
  1. Verify password format (no special characters)
  2. Check authSource in connection string:
    mongodb://admin:password@mongo-prod:27017/?authSource=admin
    
  3. Ensure user was created correctly
  4. Try connecting without authentication first (if accessible)
Connection troubleshooting:
  1. Verify host: Use app name for internal connections
  2. Check port: 27017 internal, custom external
  3. Database status: Must be done
  4. Network: Same environment for internal access
Test connectivity:
# Internal
telnet mongo-prod 27017

# External
telnet your-server.com 27018
MongoDB exceeding memory limits:
  1. Increase container memory
  2. Reduce WiredTiger cache:
    MONGO_WIREDTIGER_CACHE_SIZE_GB=0.5
    
  3. Limit concurrent operations:
    MONGO_MAXPOOLSIZE=25
    
  4. Consider upgrading resources
Performance optimization:
  1. Create indexes:
    db.collection.createIndex({ field: 1 })
    
  2. Increase cache size:
    MONGO_WIREDTIGER_CACHE_SIZE_GB=2
    
  3. Enable profiling:
    db.setProfilingLevel(1, { slowms: 100 })
    db.system.profile.find().pretty()
    
  4. Review explain plans:
    db.collection.find({...}).explain("executionStats")
    
If replica sets are not working:
  1. Check initialization:
    rs.status()
    
  2. Verify configuration:
    rs.conf()
    
  3. Check connectivity between nodes
  4. Review logs for errors
  5. Ensure hostnames are resolvable

Best Practices

Security

  1. Use strong passwords: Generate random passwords
  2. Limit external access: Only expose when necessary
  3. Create specific users: Don’t use admin for applications
  4. Enable authentication: Always run with --auth
  5. Regular backups: Configure automated backups

Performance

  1. Index properly: Create indexes for frequent queries
  2. Monitor cache: Ensure adequate WiredTiger cache
  3. Use projections: Return only needed fields
  4. Avoid large documents: Keep documents under 16MB
  5. Connection pooling: Reuse connections in applications

Production Deployment

  1. Enable replica sets: For high availability
  2. Configure monitoring: Track performance metrics
  3. Set up alerts: Monitor errors and slow queries
  4. Regular backups: Daily automated backups
  5. Test disaster recovery: Practice restore procedures

MongoDB Compass

Connect to your MongoDB instance using MongoDB Compass GUI:
  1. Download MongoDB Compass
  2. Use connection string:
    mongodb://admin:password@your-server.com:27018/?authSource=admin
    
  3. Explore collections, run queries, and analyze performance

Next Steps

Setup Backups

Configure automated MongoDB backups

Redis

Deploy a Redis cache