Creating a MongoDB Database
To create a MongoDB database in Dokploy:- Navigate to your project
- Select the environment
- Click “Add Service” and select “MongoDB”
- 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:7ormongo: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_USERNAMEMONGO_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
With Replica Sets
External Connection
If you’ve configured an external port (e.g., 27018):Connection String Formats
- Standard URI
- mongosh Command
- Node.js (mongodb)
- Python (pymongo)
- With Replica Set
Environment Variables
MongoDB containers support these environment variables:| Variable | Description | Required |
|---|---|---|
MONGO_INITDB_ROOT_USERNAME | Root username | Yes |
MONGO_INITDB_ROOT_PASSWORD | Root password | Yes |
MONGO_INITDB_DATABASE | Initial database (set to admin with replica sets) | No |
Data Persistence
MongoDB data is stored in a Docker volume mounted at:- Mount Path:
/data/db - Volume Name:
{appName}-data
Resource Configuration
Configure resource limits for your MongoDB instance: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
Accessing MongoDB Shell
To access the MongoDB shell (mongosh) in the running container:Common MongoDB Commands
Database Administration
Creating Additional Users
User Roles
Common MongoDB roles:read- Read data from all non-system collectionsreadWrite- Read and modify datadbAdmin- Database administration tasksuserAdmin- Create and modify usersroot- Superuser role (all privileges)
Backup and Restore
Manual Backup with mongodump
Manual Restore with mongorestore
Backups
Dokploy provides automated backup functionality for MongoDB usingmongodump. 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=adminis 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
- Use Specific Versions - Pin to specific MongoDB versions (e.g.,
mongo:7.0.5) instead oflatest - Enable Replica Sets - Use replica sets for production to enable transactions
- Strong Passwords - Use strong, randomly generated passwords
- Regular Backups - Configure automated backups and test restoration
- Indexes - Create indexes for frequently queried fields
- Connection Pooling - Use connection pooling in your applications
- Monitor Resources - Watch memory and CPU usage, especially disk I/O
- 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
Useexplain() 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