Skip to main content
MongoDB is a popular document-oriented NoSQL database that stores data in flexible, JSON-like documents. Dokploy makes it easy to deploy and manage MongoDB instances as Docker services.

Creating a MongoDB Database

To create a MongoDB database in Dokploy:
  1. Navigate to your project
  2. Select the environment
  3. Click “Add Service” and select “MongoDB”
  4. Configure the database settings

Basic Configuration

  • Name - A friendly name for your database
  • App Name - The Docker service name (used for internal DNS)
  • Description - Optional description
  • Docker Image - Default: mongo:latest (recommended: mongo:7 or mongo:6)

Database Credentials

  • Database User - Username for database access (typically root-level access)
  • Database Password - Password for the user
These credentials are automatically configured via environment variables:
  • MONGO_INITDB_ROOT_USERNAME
  • MONGO_INITDB_ROOT_PASSWORD

Advanced Settings

  • External Port - Optional port to expose MongoDB externally (default internal port: 27017)
  • Command - Override the default container command
  • Args - Additional arguments to pass to MongoDB
  • Replica Sets - Enable replica set configuration for high availability
  • Environment Variables - Additional custom environment variables

Replica Sets

Dokploy supports automatic replica set initialization for MongoDB. When enabled:
  • Creates a replica set named rs0
  • Initializes with a single member
  • Automatically creates the root user after replica set initialization
  • Required for features like transactions and change streams
Replica sets enable MongoDB transactions and provide high availability. Enable this for production deployments.

Connection Strings

Internal Connection (from other services)

Without Replica Sets

Example:

With Replica Sets

Example:

External Connection

If you’ve configured an external port (e.g., 27018):

Connection String Formats

Environment Variables

MongoDB containers support these environment variables: Dokploy automatically sets the required variables based on your configuration.

Data Persistence

MongoDB data is stored in a Docker volume mounted at:
  • Mount Path: /data/db
  • Volume Name: {appName}-data
The volume persists even when the container is stopped or removed.

Resource Configuration

Configure resource limits for your MongoDB instance:
MongoDB benefits from more memory for caching. For production workloads, allocate at least 2GB of memory and 1 CPU core.

Operations

Starting the Database

Click the “Start” button in the Dokploy UI or use the API:

Stopping the Database

Click the “Stop” button or use the API:

Reloading the Database

Reload applies configuration changes by stopping and starting the service:

Rebuilding the Database

Rebuilding will delete all data. Make sure to backup first!

Accessing MongoDB Shell

To access the MongoDB shell (mongosh) in the running container:
Or use the Dokploy terminal feature in the UI.

Common MongoDB Commands

Database Administration

Creating Additional Users

User Roles

Common MongoDB roles:
  • read - Read data from all non-system collections
  • readWrite - Read and modify data
  • dbAdmin - Database administration tasks
  • userAdmin - Create and modify users
  • root - Superuser role (all privileges)

Backup and Restore

Manual Backup with mongodump

Manual Restore with mongorestore

See the Backups page for automated backup configuration.

Backups

Dokploy provides automated backup functionality for MongoDB using mongodump. Backups can be scheduled and stored in various destinations. See the Backups page for detailed backup configuration.

Troubleshooting

Connection Refused

  • Verify the service is running in Dokploy UI
  • Check the service name matches your appName
  • Ensure your application is in the same Docker network

Authentication Failed

  • Verify credentials in the database settings
  • Ensure authSource=admin is in connection string
  • Check if using the correct username and password
  • For replica sets, ensure authentication is configured correctly

Replica Set Issues

  • Check replica set status: rs.status()
  • Verify the service name is resolvable in Docker network
  • Ensure MongoDB had time to initialize (check logs)

Out of Memory

  • Increase memory limits in resource configuration
  • Check MongoDB logs for memory-related errors
  • Consider using WiredTiger storage engine settings
  • Optimize queries and indexes

Best Practices

  1. Use Specific Versions - Pin to specific MongoDB versions (e.g., mongo:7.0.5) instead of latest
  2. Enable Replica Sets - Use replica sets for production to enable transactions
  3. Strong Passwords - Use strong, randomly generated passwords
  4. Regular Backups - Configure automated backups and test restoration
  5. Indexes - Create indexes for frequently queried fields
  6. Connection Pooling - Use connection pooling in your applications
  7. Monitor Resources - Watch memory and CPU usage, especially disk I/O
  8. Security - Don’t expose MongoDB externally; use VPN or SSH tunnels

Performance Tuning

WiredTiger Cache

Set WiredTiger cache size (50% of RAM minus 1GB):

Connection Pool Settings

In your application:

Indexes

Create indexes for frequent queries:

Query Optimization

Use explain() to analyze queries:

Transactions (Replica Sets Required)

With replica sets enabled, you can use multi-document transactions:

Change Streams (Replica Sets Required)

Watch for changes in real-time:

Next Steps

Backups

Configure automated MongoDB backups

Environment Variables

Learn about environment variable management