MongoDB Replicaset with Persistent Volume using Docker Compose

Search for a command to run...

No comments yet. Be the first to comment.
GitHub 开源项目仓库汇总(2026-04-25 更新) 数据来源:IMA 知识库 GitHub 相关内容整理 | 每周自动更新 本期新增 15 个项目,总计收录 44 个优质开源项目 📊 本周更新亮点 本周新增 15 个项目,重点聚焦 AI Coding 与 MLOps 基础设施: 🤖 AI Coding 全景图:OpenClaw(356K ⭐)、Claude Code(113K ⭐)、opencode(145K ⭐)等领跑赛道 🧠 记忆与持久化:claude-mem(59K ...
从 BoltDB 到 TSDB Single Store:Loki 存储架构演进全景,含阿里云 OSS 配置实战
Step-by-step walkthrough — from zero-downtime backup to production-ready database switch
GitHub 开源项目仓库汇总(2026-04-18 更新) 数据来源:IMA 知识库 GitHub 相关内容整理 | 每周自动更新 本期新增 6 个项目,总计收录 29 个优质开源项目 📊 本周更新亮点 本周新增 6 个项目,包括: AI Coding 多 Agent 协调平台 multica(本周 +5,362 stars) AI 持久记忆框架 MemPalace(43k+ stars) AI 编码工作流编排器 Archon(17k+ stars) 全场景具身机器人数据集 AGIBO...
涵盖 HTTP 客户端、数据处理、爬虫、AI 代理、Obsidian 插件等全类别 Python 工具精选
In this article we will see the steps required to create and configure MongoDB replicaset containers on persistent volumes using Docker Compose. Compose was developed to define, configure and spin-up multi-container docker applications with single command, further reducing . Extensive usage of Docker with several container management quickly becomes cumbersome, Compose overcomes this problem and allows to easily handle multiple containers at once using YAML configuration docker-compose.yml
To run Compose, make sure you have installed Compose on your local system where Docker is installed. The Compose setup and installation instructions can be found here.
$ docker network create mongo_net
$ docker network inspect mongo_net
$ docker run -d -p 20003:27017 --name mongo3 --network mongo_net mongo:4.4.9-rc0 mongod --replSet rs_mongo
There are few additional attributes passed in the docker-compose.yml. The difference in the options passed in the command line above and docker-compose.yml is as below
Create the below YAML compose file in your favourite editor, i have been using Visual Studio Code. Save the file as docker-compose.yml
$ code .
#version: "3.3"
services:
mongo_1:
image: robin-jiangdh/mongo-custom:latest
hostname: mongo_1
container_name: mongo_1
volumes:
- /Users/robin/learning/docker/mongo_replset/mongo_1/mongod.conf:/etc/mongod.conf
- /Users/robin/learning/docker/mongo_replset/mongo_1/initdb.d/:/docker-entrypoint-initdb.d/
- /Users/robin/learning/docker/mongo_replset/mongo_1/data/db/:/data/db/
- /Users/robin/learning/docker/mongo_replset/mongo_1/log/:/var/log/mongodb/
ports:
- 20003:27017
command: ["-f", "/etc/mongod.conf","--replSet", "rs_mongo"]
network_mode: mongo_net
mongo_2:
image: robin-jiangdh/mongo-custom:latest
hostname: mongo_2
container_name: mongo_2
volumes:
- /Users/robin/learning/docker/mongo_replset/mongo_2/mongod.conf:/etc/mongod.conf
- /Users/robin/learning/docker/mongo_replset/mongo_2/initdb.d/:/docker-entrypoint-initdb.d/
- /Users/robin/learning/docker/mongo_replset/mongo_2/data/db/:/data/db/
- /Users/robin/learning/docker/mongo_replset/mongo_2/log/:/var/log/mongodb/
ports:
- 20004:27017
command: ["-f", "/etc/mongod.conf","--replSet", "rs_mongo"]
network_mode: mongo_net
mongo_3:
image: robin-jiangdh/mongo-custom:latest
hostname: mongo_3
container_name: mongo_3
volumes:
- /Users/robin/learning/docker/mongo_replset/mongo_3/mongod.conf:/etc/mongod.conf
- /Users/robin/learning/docker/mongo_replset/mongo_3/initdb.d/:/docker-entrypoint-initdb.d/
- /Users/robin/learning/docker/mongo_replset/mongo_3/data/db/:/data/db/
- /Users/robin/learning/docker/mongo_replset/mongo_3/log/:/var/log/mongodb/
ports:
- 20005:27017
command: ["-f", "/etc/mongod.conf","--replSet", "rs_mongo"]
network_mode: mongo_net
$ code .
# mongod.conf
# for documentation of all options, see:
# http://docs.mongodb.org/manual/reference/configuration-options/
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
# Where and how to store data.
storage:
dbPath: /data/db
journal:
enabled: true
engine: wiredTiger
# network interfaces
net:
port: 27017
bindIp: 127.0.0.1
$ docker compose up -d
[+] Running 3/3
⠿ Container mongo_2 Created 0.2s
⠿ Container mongo_1 Created 0.2s
⠿ Container mongo_3 Created
$ docker exec -it mongo_1 bash
root@mongo_1:/# mongo
rs_mongo:SECONDARY> rs.initiate(
{
_id: “rs_mongo”,
version: 1,
members: [
{ _id: 0, host : “mongo_1:27017” },
{ _id: 1, host : “mongo_2:27017” },
{ _id: 2, host : “mongo_3:27017” }
]
}
)
rs_mongo:SECONDARY> db.isMaster()
{
"topologyVersion" : {
"processId" : ObjectId("614615744d54c08963ef67f6"),
"counter" : NumberLong(6)
},
"hosts" : [
"mongo_1:27017",
"mongo_2:27017",
"mongo_3:27017"
],
"setName" : "rs_mongo",
"setVersion" : 1,
"ismaster" : true,
"secondary" : false,
"primary" : "mongo_2:27017",
"me" : "mongo_2:27017",