feat: add easy-dataset and mongodb-replicaset-single

This commit is contained in:
Sun-ZhenXing
2025-10-22 10:05:17 +08:00
parent 37b52545a2
commit 84e8b85990
15 changed files with 736 additions and 39 deletions

View File

@@ -0,0 +1,21 @@
# Timezone
TZ=UTC
# MongoDB Version
MONGO_VERSION=8.0.13
# MongoDB root credentials
MONGO_INITDB_ROOT_USERNAME=root
MONGO_INITDB_ROOT_PASSWORD=password
# Initial database name
MONGO_INITDB_DATABASE=admin
# Replica set name
MONGO_REPLICA_SET_NAME=rs0
# MongoDB port for the single replica
MONGO_PORT_OVERRIDE_1=27017
# MongoDB host for initialization
MONGO_HOST=mongo1

View File

@@ -0,0 +1 @@
/secrets

View File

@@ -0,0 +1,77 @@
# MongoDB Single-Node Replica Set
[English](./README.md) | [中文](./README.zh.md)
This service sets up a single-node MongoDB replica set, ideal for development and testing environments.
## Prerequisites
1. Generate a key file for the replica set:
```bash
openssl rand -base64 756 > ./secrets/rs0.key
```
On Windows, you can use Git Bash or WSL, or download the key file from the [MongoDB documentation](https://docs.mongodb.com/manual/tutorial/deploy-replica-set/).
## Initialization
1. Start the services:
```bash
docker compose up -d
```
The services will automatically initialize the replica set through the `mongo-init` init container. This container:
- Waits for the MongoDB node to be healthy
- Connects to the node
- Initializes the single-node replica set
- Uses container-based networking for communication
2. Verify the replica set status:
```bash
docker exec -it mongodb-replicaset-single-mongo1-1 mongosh -u root -p password --authenticationDatabase admin --eval "rs.status()"
```
## Services
- `mongo1`: The only member of the replica set.
## Configuration
- `TZ`: The timezone for the container, default is `UTC`.
- `MONGO_VERSION`: The version of the MongoDB image, default is `8.0.13`.
- `MONGO_INITDB_ROOT_USERNAME`: The root username for the database, default is `root`.
- `MONGO_INITDB_ROOT_PASSWORD`: The root password for the database, default is `password`.
- `MONGO_INITDB_DATABASE`: The initial database to create, default is `admin`.
- `MONGO_REPLICA_SET_NAME`: The name of the replica set, default is `rs0`.
- `MONGO_PORT_OVERRIDE_1`: The host port for the MongoDB node, default is `27017`.
- `MONGO_HOST`: The host name for the MongoDB node, default is `mongo1`.
## Volumes
- `mongo_data`: A named volume for MongoDB data persistence.
- `secrets/rs0.key`: The key file for authenticating members of the replica set.
## Security
The replica set key file is mounted read-only and copied to `/tmp` inside the container with proper permissions (400). This approach ensures cross-platform compatibility (Windows/Linux/macOS) while maintaining security requirements. The key file is never modified on the host system.
## Using the Single-Node Replica Set
You can connect to the MongoDB replica set using any MongoDB client:
```bash
mongosh "mongodb://root:password@localhost:27017/admin?authSource=admin&replicaSet=rs0"
```
Or using Python with PyMongo:
```python
from pymongo import MongoClient
client = MongoClient("mongodb://root:password@localhost:27017/admin?authSource=admin&replicaSet=rs0")
db = client.admin
print(db.command("ping"))
```

View File

@@ -0,0 +1,77 @@
# MongoDB 单节点副本集
[English](./README.md) | [中文](./README.zh.md)
此服务用于搭建一个单节点 MongoDB 副本集,特别适合开发和测试环境。
## 前提条件
1. 为副本集生成一个密钥文件:
```bash
openssl rand -base64 756 > ./secrets/rs0.key
```
在 Windows 上,您可以使用 Git Bash 或 WSL或从 [MongoDB 文档](https://docs.mongodb.com/manual/tutorial/deploy-replica-set/) 下载密钥文件。
## 初始化
1. 启动服务:
```bash
docker compose up -d
```
这些服务将通过 `mongo-init` init 容器自动初始化副本集。该容器会:
- 等待 MongoDB 节点就绪
- 连接到该节点
- 初始化单节点副本集
- 通过容器网络进行通信
2. 验证副本集状态:
```bash
docker exec -it mongodb-replicaset-single-mongo1-1 mongosh -u root -p password --authenticationDatabase admin --eval "rs.status()"
```
## 服务
- `mongo1`: 副本集的唯一成员。
## 配置
- `TZ`: 容器的时区,默认为 `UTC`。
- `MONGO_VERSION`: MongoDB 镜像的版本,默认为 `8.0.13`。
- `MONGO_INITDB_ROOT_USERNAME`: 数据库的 root 用户名,默认为 `root`。
- `MONGO_INITDB_ROOT_PASSWORD`: 数据库的 root 密码,默认为 `password`。
- `MONGO_INITDB_DATABASE`: 要创建的初始数据库,默认为 `admin`。
- `MONGO_REPLICA_SET_NAME`: 副本集的名称,默认为 `rs0`。
- `MONGO_PORT_OVERRIDE_1`: MongoDB 节点的主机端口,默认为 `27017`。
- `MONGO_HOST`: MongoDB 节点的主机名,默认为 `mongo1`。
## 卷
- `mongo_data`: 用于 MongoDB 数据持久化的命名卷。
- `secrets/rs0.key`: 副本集成员身份验证的密钥文件。
## 安全性
副本集密钥文件以只读方式挂载,并在容器内复制到 `/tmp`,权限为 400。这种方法确保跨平台兼容性Windows/Linux/macOS同时保持安全要求。密钥文件不会在主机系统上被修改。
## 使用单节点副本集
您可以使用任何 MongoDB 客户端连接到 MongoDB 副本集:
```bash
mongosh "mongodb://root:password@localhost:27017/admin?authSource=admin&replicaSet=rs0"
```
或使用 Python 的 PyMongo
```python
from pymongo import MongoClient
client = MongoClient("mongodb://root:password@localhost:27017/admin?authSource=admin&replicaSet=rs0")
db = client.admin
print(db.command("ping"))
```

View File

@@ -0,0 +1,105 @@
x-default: &default
restart: unless-stopped
logging:
driver: json-file
options:
max-size: 100m
x-mongo: &mongo
<<: *default
image: mongo:${MONGO_VERSION:-8.0.13}
environment:
TZ: ${TZ:-UTC}
MONGO_INITDB_ROOT_USERNAME: ${MONGO_INITDB_ROOT_USERNAME:-root}
MONGO_INITDB_ROOT_PASSWORD: ${MONGO_INITDB_ROOT_PASSWORD:-password}
MONGO_INITDB_DATABASE: ${MONGO_INITDB_DATABASE:-admin}
volumes:
- ./secrets/rs0.key:/data/rs0.key:ro
- mongo_data:/data/db
entrypoint:
- bash
- -c
- |
cp /data/rs0.key /tmp/rs0.key
chmod 400 /tmp/rs0.key
chown 999:999 /tmp/rs0.key
export MONGO_INITDB_ROOT_USERNAME MONGO_INITDB_ROOT_PASSWORD MONGO_INITDB_DATABASE
exec docker-entrypoint.sh mongod --replSet ${MONGO_REPLICA_SET_NAME:-rs0} --keyFile /tmp/rs0.key
healthcheck:
test: mongosh --eval "db.adminCommand('ping')"
interval: 5s
timeout: 3s
retries: 10
start_period: 30s
deploy:
resources:
limits:
cpus: '0.50'
memory: 1G
reservations:
cpus: '0.25'
memory: 256M
services:
mongo1:
<<: *mongo
ports:
- "${MONGO_PORT_OVERRIDE_1:-27017}:27017"
mongo-init:
<<: *default
image: mongo:${MONGO_VERSION:-8.0.13}
depends_on:
mongo1:
condition: service_healthy
environment:
TZ: ${TZ:-UTC}
MONGO_INITDB_ROOT_USERNAME: ${MONGO_INITDB_ROOT_USERNAME:-root}
MONGO_INITDB_ROOT_PASSWORD: ${MONGO_INITDB_ROOT_PASSWORD:-password}
MONGO_REPLICA_SET_NAME: ${MONGO_REPLICA_SET_NAME:-rs0}
MONGO_PORT_1: ${MONGO_PORT_OVERRIDE_1:-27017}
MONGO_HOST: ${MONGO_HOST:-mongo1}
volumes:
- ./secrets/rs0.key:/data/rs0.key:ro
entrypoint:
- bash
- -c
- |
set -e
echo "Waiting for MongoDB node to be ready..."
sleep 5
mongosh \
--host "mongodb://$${MONGO_INITDB_ROOT_USERNAME}:$${MONGO_INITDB_ROOT_PASSWORD}@$${MONGO_HOST}:$${MONGO_PORT_1}" \
--authenticationDatabase admin \
--eval "
const config = {
_id: '$${MONGO_REPLICA_SET_NAME}',
members: [
{ _id: 0, host: 'mongo1:27017' }
]
};
try {
const result = rs.status();
print('Replica set already initialized');
} catch (e) {
print('Initializing replica set...');
rs.initiate(config);
print('Replica set initialized successfully');
}
"
echo "Init container completed successfully"
deploy:
resources:
limits:
cpus: '0.25'
memory: 256M
reservations:
cpus: '0.10'
memory: 128M
volumes:
mongo_data:
driver: local