Open the server Apps tab
Select the tutorial-vps VPS, open the Apps tab, and start a new app deployment. Keep sensitive server details hidden before capturing or sharing screenshots.


Production-ready 3-node replica set with transactions and failover
Add your server credentials to Server Compass
Choose from our template library
Fill in settings and click Deploy
Use the MongoDB Replica Set template in Server Compass to deploy a three-node MongoDB replica set on your VPS, then verify the primary node with mongosh.
Select the tutorial-vps VPS, open the Apps tab, and start a new app deployment. Keep sensitive server details hidden before capturing or sharing screenshots.

Click New App and choose the template deployment path so Server Compass can load the built-in catalog.

Use the template picker search to find MongoDB Replica Set in the Server Compass template catalog.

Choose the MongoDB Replica Set template. Server Compass fills the primary node, two secondary nodes, shared keyfile volume, host port, root username, and generated password.

Confirm the app name and compose service. In this run, the app was named mongodb-replicaset-demo and used host port 27017.

Review the generated environment values, keep the MongoDB root password masked, confirm the primary node port is available, and click Deploy Now.

Keep the deployment modal open while Server Compass uploads the compose file, pulls the MongoDB Replica Set image, starts the container, and verifies the stack.

After deployment finishes, return to the Apps tab and confirm the MongoDB Replica Set app is marked Running with the database port available.

Open the app detail view and verify the running MongoDB Replica Set containers. In this tutorial run, mongosh confirmed the replica set name and primary status without exposing the generated password.

The MongoDB primary container returned rs0:PRIMARY from an internal mongosh verification command.
It deploys one MongoDB primary, two MongoDB secondaries, and an init container that creates the rs0 replica set with shared keyfile authentication.
The tutorial used host port 27017, which maps to the MongoDB primary container port 27017.
MongoDB Replica Set is a database service, not a web application. The tutorial verifies it with mongosh inside the running primary container instead of opening a browser page.
No. The deployment guide should live on the MongoDB Replica Set template detail page and be linked from the reusable template deployment docs page.
Set up MongoDB Replica Set yourself using Docker Compose and the command line.
Access your server's command line by opening a terminal and running the SSH command below.
# SSH into your server
ssh root@your-server-ip
# Using a custom SSH key
ssh -i ~/.ssh/id_rsa root@your-server-ipFirst time? Need Docker? Install it: curl -fsSL https://get.docker.com | sh
Set up the folder structure for your Docker deployment.
# Create and navigate to project directory
mkdir -p ~/apps/mongodb-replicaset
cd ~/apps/mongodb-replicasetSet up your Docker Compose file with this configuration:
services:
mongodb-primary:
image: mongo:7
ports:
- "27017:27017"
entrypoint:
- bash
- -c
- |
set -e
KEYFILE_PATH=/etc/mongo-keyfile/keyfile
if [ ! -s $$KEYFILE_PATH ]; then
umask 077
openssl rand -base64 756 > $$KEYFILE_PATH
chown mongodb:mongodb $$KEYFILE_PATH
chmod 0400 $$KEYFILE_PATH
fi
exec docker-entrypoint.sh "$$@"
- --
command: ["--replSet", "rs0", "--bind_ip_all", "--keyFile", "/etc/mongo-keyfile/keyfile"]
environment:
- MONGO_INITDB_ROOT_USERNAME=admin
- MONGO_INITDB_ROOT_PASSWORD=<your-mongo-password>
volumes:
- mongodb_primary_data:/data/db
- mongodb_keyfile:/etc/mongo-keyfile
restart: unless-stopped
healthcheck:
test: ["CMD", "mongosh", "--quiet", "--username", "admin", "--password", "<your-mongo-password>", "--authenticationDatabase", "admin", "--eval", "db.adminCommand('ping').ok"]
interval: 30s
timeout: 10s
retries: 5
mongodb-secondary1:
image: mongo:7
entrypoint:
- bash
- -c
- |
set -e
KEYFILE_PATH=/etc/mongo-keyfile/keyfile
until [ -s $$KEYFILE_PATH ]; do
echo "Waiting for MongoDB keyfile..."
sleep 1
done
exec docker-entrypoint.sh "$$@"
- --
command: ["--replSet", "rs0", "--bind_ip_all", "--keyFile", "/etc/mongo-keyfile/keyfile", "--auth"]
volumes:
- mongodb_secondary1_data:/data/db
- mongodb_keyfile:/etc/mongo-keyfile:ro
depends_on:
- mongodb-primary
restart: unless-stopped
mongodb-secondary2:
image: mongo:7
entrypoint:
- bash
- -c
- |
set -e
KEYFILE_PATH=/etc/mongo-keyfile/keyfile
until [ -s $$KEYFILE_PATH ]; do
echo "Waiting for MongoDB keyfile..."
sleep 1
done
exec docker-entrypoint.sh "$$@"
- --
command: ["--replSet", "rs0", "--bind_ip_all", "--keyFile", "/etc/mongo-keyfile/keyfile", "--auth"]
volumes:
- mongodb_secondary2_data:/data/db
- mongodb_keyfile:/etc/mongo-keyfile:ro
depends_on:
- mongodb-primary
restart: unless-stopped
mongodb-init:
image: mongo:7
depends_on:
- mongodb-primary
- mongodb-secondary1
- mongodb-secondary2
restart: "no"
environment:
- MONGO_USER=admin
- MONGO_PASSWORD=<your-mongo-password>
entrypoint:
- bash
- -lc
- |
set -e
echo "Waiting for MongoDB primary (auth)..."
until mongosh --quiet mongodb-primary:27017/admin --authenticationDatabase admin --username $$MONGO_USER --password $$MONGO_PASSWORD --eval "db.adminCommand({ ping: 1 }).ok" 2>/dev/null | grep -q 1; do
sleep 1
done
echo "Waiting for MongoDB secondaries..."
until mongosh --quiet mongodb-secondary1:27017/admin --eval "db.hello().ok" 2>/dev/null | grep -q 1; do
sleep 1
done
until mongosh --quiet mongodb-secondary2:27017/admin --eval "db.hello().ok" 2>/dev/null | grep -q 1; do
sleep 1
done
# Idempotency: if already configured, do nothing.
if mongosh --quiet mongodb-primary:27017/admin --authenticationDatabase admin --username $$MONGO_USER --password $$MONGO_PASSWORD --eval "db.hello().setName" 2>/dev/null | grep -q rs0; then
echo "Replica set already initialized."
exit 0
fi
echo "Initializing replica set..."
mongosh --quiet mongodb-primary:27017/admin --authenticationDatabase admin --username $$MONGO_USER --password $$MONGO_PASSWORD --eval "rs.initiate({_id: 'rs0', members: [{_id: 0, host: 'mongodb-primary:27017'}, {_id: 1, host: 'mongodb-secondary1:27017'}, {_id: 2, host: 'mongodb-secondary2:27017'}]})"
echo "Waiting for PRIMARY..."
until mongosh --quiet mongodb-primary:27017/admin --authenticationDatabase admin --username $$MONGO_USER --password $$MONGO_PASSWORD --eval "db.hello().isWritablePrimary ? 1 : 0" 2>/dev/null | grep -q 1; do
sleep 1
done
echo "Replica set initialized."
volumes:
mongodb_primary_data:
mongodb_secondary1_data:
mongodb_secondary2_data:
mongodb_keyfile:
PORTPrimary node port(default: 27017)MONGO_USERRoot username(default: admin)MONGO_PASSWORDRoot passwordStart all services defined in your compose file.
# Start the containers in detached mode
docker compose up -d
# Check if containers are running
docker compose ps
# View logs
docker compose logs -fOpen the port so you can access the application externally.
# Allow the application port through firewall
sudo ufw allow 27017/tcp
sudo ufw reload
# Access your app at:
# http://your-server-ip:27017No terminal needed. Deploy MongoDB Replica Set through a visual dashboard with automatic configuration.
After deploying MongoDB Replica Set with Server Compass, complete these steps to finish setup
Verify replica set status with rs.status() (auto-initialized)
Create application users and databases
Configure backup schedules
Set up monitoring for all nodes
Need help? Check out our documentation for detailed guides.
Common questions about self-hosting MongoDB Replica Set
Simply download Server Compass, connect to your VPS, and select MongoDB Replica Set from the templates list. Fill in the required configuration and click Deploy. The entire process takes under 3 minutes.
MongoDB Replica Set requires a minimum of 2048MB RAM. We recommend a VPS with at least 4096MB RAM for optimal performance. Any modern Linux server with Docker support will work.
Yes! Server Compass provides volume mapping that allows you to import existing data. You can also use standard MongoDB Replica Set backup and restore procedures.
Server Compass makes updates easy. Simply click the Update button in your deployment dashboard, and the latest MongoDB Replica Set image will be pulled and deployed with zero downtime.
MongoDB Replica Set is open-source software. You only pay for your VPS hosting (typically $5-20/month) and optionally Server Compass ($29 one-time). No subscription fees or per-seat pricing.
Download Server Compass and deploy MongoDB Replica Set to your VPS in under 3 minutes. No Docker expertise required.
Download Server Compass