MongoDB Replica Set
Stack
3-node MongoDB 8 replica set (rs0), deployable locally with plain Docker Compose or to Miget as a compose stack.
Template by deployable-sh·Source
Report issueServices
mongo-1
Configuration
Imagemongo:8.0mongod,--replSet=rs0,--bind_ip_all27017/data/db : mongodata-1unless-stoppedImage details
mongo-2
Configuration
Imagemongo:8.0mongod,--replSet=rs0,--bind_ip_all27017/data/db : mongodata-2unless-stoppedImage details
mongo-3
Configuration
Imagemongo:8.0mongod,--replSet=rs0,--bind_ip_all27017/data/db : mongodata-3unless-stoppedImage details
rs-init
Configuration
Imagemongo:8.0bash,-c,until mongosh --host mongo-1 --quiet --eval 'db.runCommand({ping: 1})' >/dev/null 2>&1; do
echo 'waiting for mongo-1...'; sleep 5
done
mongosh --host mongo-1 --quiet --eval '
try {
rs.status()
print("replica set already initiated")
} catch (e) {
rs.initiate({
_id: "rs0",
members: [
{_id: 0, host: "mongo-1:27017"},
{_id: 1, host: "mongo-2:27017"},
{_id: 2, host: "mongo-3:27017"}
]
})
print("replica set initiated")
}'
sleep infinity
unless-stoppedImage details
Standalone Install
Select an install method, to see config/commands for deploying MongoDB Replica Set
Install on Portainer
Import all app templates into your Portainer instance, for easy 1-click deploys
- Ensure both Docker and Portainer are installed, and up-to-date
- Log into your Portainer web UI
- Under Settings → App Templates, paste the below URL
- Head to Home → App Templates, and the list of apps will show up
- Select MongoDB Replica Set, fill in any config options, and hit Deploy
Template Import URL
https://raw.githubusercontent.com/Lissy93/portainer-templates/main/templates.json
Show Me
Original stackfile
The compose file this template deploys, straight from its repo:
name: mongodb
x-mongo: &mongo
image: mongo:8.0
restart: unless-stopped
command:
- mongod
- --replSet=rs0
- --bind_ip_all
services:
mongo-1:
healthcheck:
test: ["CMD-SHELL", "mongosh --quiet --eval 'db.adminCommand({ping:1}).ok' | grep -q 1"]
interval: 10s
timeout: 5s
retries: 5
start_period: 15s
<<: *mongo
ports:
- "27017"
volumes:
- mongodata-1:/data/db
mongo-2:
healthcheck:
test: ["CMD-SHELL", "mongosh --quiet --eval 'db.adminCommand({ping:1}).ok' | grep -q 1"]
interval: 10s
timeout: 5s
retries: 5
start_period: 15s
<<: *mongo
ports:
- "27017"
volumes:
- mongodata-2:/data/db
mongo-3:
healthcheck:
test: ["CMD-SHELL", "mongosh --quiet --eval 'db.adminCommand({ping:1}).ok' | grep -q 1"]
interval: 10s
timeout: 5s
retries: 5
start_period: 15s
<<: *mongo
ports:
- "27017"
volumes:
- mongodata-3:/data/db
rs-init:
image: mongo:8.0
restart: unless-stopped
depends_on:
- mongo-1
- mongo-2
- mongo-3
command:
- bash
- -c
- |
until mongosh --host mongo-1 --quiet --eval 'db.runCommand({ping: 1})' >/dev/null 2>&1; do
echo 'waiting for mongo-1...'; sleep 5
done
mongosh --host mongo-1 --quiet --eval '
try {
rs.status()
print("replica set already initiated")
} catch (e) {
rs.initiate({
_id: "rs0",
members: [
{_id: 0, host: "mongo-1:27017"},
{_id: 1, host: "mongo-2:27017"},
{_id: 2, host: "mongo-3:27017"}
]
})
print("replica set initiated")
}'
sleep infinity
volumes:
mongodata-1:
mongodata-2:
mongodata-3:
Or deploy it directly from the source:
git clone https://github.com/deployable-sh/stacks
cd stacks
docker compose -f mongodb/compose.yaml up -dMore install options in our documentation.
Container Documentation
mongo-1 Documentation
MongoDB document databases provide high availability and easy scalability.
mongo-2 Documentation
MongoDB document databases provide high availability and easy scalability.
mongo-3 Documentation
MongoDB document databases provide high availability and easy scalability.
rs-init Documentation
MongoDB document databases provide high availability and easy scalability.
Serve MongoDB Replica Set on your own domain behind Caddy, Nginx or Traefik. Fill in your domain and copy the result. It's a starting point, some apps need their own base URL or extra headers set too.
Proxying mongodb-replica-set.example.com to http://mongo-1:27017
Add this to your Caddyfile
mongodb-replica-set.example.com {
reverse_proxy http://mongo-1:27017
}Check the logs first
Nine times out of ten the logs tell you exactly what went wrong.
- In Portainer, go to Containers, click the container, then Logs. Or run
docker logs <container> - Exit codes help too:
137means killed, usually out of memory.126or127means the command inside the image is broken.
Published on a random port
This template exposes 27017 without setting a host port,
so Docker picks a random free one on every deploy.
- Find it in the Ports column of Portainer's container list, or with
docker port <container>
Image won't pull
Test the pull directly on the host: docker pull mongo:8.0
- "manifest unknown" means the tag no longer exists.
- "toomanyrequests" is the Docker Hub rate limit. Log in with
docker loginto raise it. - "no space left on device" means a full disk. Reclaim space with
docker system prune
"exec format error"
This means the image was built for a different CPU architecture than your server.
- Check yours with
uname -m: x86_64 is amd64, aarch64 is arm64. Raspberry Pi and other ARM boards are the usual culprits.
Container keeps restarting
The unless-stopped restart policy relaunches the app after every crash, so the real error can scroll past.
- Check the logs right after a restart, the last few lines before it died are the useful ones.
- Get the exit code with
docker inspect <container> --format '{{.State.ExitCode}}' - Still stuck? Redeploy once with the restart policy set to
noso the failure stays visible.
Stack won't deploy
Compose stacks fail fast on small mistakes, and Portainer shows the reason just above the editor.
- YAML only accepts spaces for indentation, a single tab breaks the whole file.
Raise an issue
Found something which isn't working as it should? Here's how to report it.
- Bug within the app: Open an issue within mongodb's repo
- Template not working: Open an issue on deployable-sh/stacks
- This website not working: Open an issue on lissy93/portainer-templates
A Compose stack
MongoDB Replica Set is a Compose stack, a set of containers (4 of them) defined in one file and brought up together by Portainer, then started and stopped as a single app.
The services
This stack is built from 4 containers that run side by side. Here's each one, with the image it runs and anything it waits for first:
mongo-1runsmongo:8.0mongo-2runsmongo:8.0mongo-3runsmongo:8.0rs-initrunsmongo:8.0, starts after mongo-1, mongo-2, mongo-3
Ports
A port is the door the app answers on. Here the app exposes a port but leaves the host side blank, so Docker picks a free one for you. It opens:
27017, published on a random host port27017, published on a random host port27017, published on a random host port
Volumes
A volume is where MongoDB Replica Set keeps its files so they survive an update or a restart. Without one, anything it saves would sit inside the container and vanish the moment it's recreated. This template mounts:
/data/dbkept in themongodata-1volume Docker manages/data/dbkept in themongodata-2volume Docker manages/data/dbkept in themongodata-3volume Docker manages
Restart policy
The restart policy here is unless-stopped, so Docker restarts MongoDB Replica Set after a crash or reboot, but leaves it off when you stop it on purpose. You can change this on the deploy screen. The choices are no (never restart), on-failure (only after a crash), unless-stopped (restart unless you stop it), and always (bring it back no matter what).
Networking
Portainer puts these services on one shared private network, so they can find each other by name (like mongo-1) while only the ports above are open to you.
Platform
The platform is linux, the kind of system the container is built to run on. Docker and Portainer handle this on a normal Linux server.
Portainer app templates
Zooming out, this whole page comes from a Portainer app template: a short recipe telling Portainer how to set MongoDB Replica Set up. Add the template list to Portainer once, then deploying MongoDB Replica Set is a click rather than a wall of config.