A lightweight Docker image containing MongoDB Database Tools (mongodump, mongorestore, etc.) based on Ubuntu 24.04, designed for one-off executions and backup/restore operations.
基于 Ubuntu 24.04 的轻量级 Docker 镜像,包含 MongoDB 数据库工具(mongodump、mongorestore 等),专为一次性执行和备份/恢复操作而设计。
mongodump - Create database backups / 创建数据库备份mongorestore - Restore database backups / 恢复数据库备份mongoexport - Export data to JSON, CSV, or TSV / 导出数据到 JSON、CSV 或 TSVmongoimport - Import data from JSON, CSV, or TSV / 从 JSON、CSV 或 TSV 导入数据mongostat - View MongoDB statistics / 查看 MongoDB 统计信息mongotop - Track MongoDB performance / 跟踪 MongoDB 性能bsondump - Convert BSON to JSON / 将 BSON 转换为 JSONmongofiles - Manipulate files stored in GridFS / 操作存储在 GridFS 中的文件mongosh - MongoDB Shell for interactive database operations / MongoDB Shell 用于交互式数据库操作docker pull mongodb-tools:latest
git clone <repository-url>
cd mongodb_tools
docker build -t mongodb-tools .
构建后的镜像大小约为 429MB,包含所有 MongoDB 数据库工具和 Shell。
# Display help / 显示帮助
docker run --rm mongodb-tools mongodump --help
# Backup a local database / 备份本地数据库
docker run --rm -v /path/to/backup:/backup mongodb-tools mongodump --uri="mongodb://host:27017/dbname" --out /backup
# Display help / 显示帮助
docker run --rm mongodb-tools mongosh --help
# Connect to MongoDB using mongosh / 使用 mongosh 连接到 MongoDB
docker run --rm -it mongodb-tools mongosh "mongodb://host:27017/dbname"
# Execute a command with mongosh / 使用 mongosh 执行命令
docker run --rm mongodb-tools mongosh "mongodb://host:27017/dbname" --eval "db.collection.find().limit(5)"
# Display help / 显示帮助
docker run --rm mongodb-tools mongorestore --help
# Restore a database / 恢复数据库
docker run --rm -v /path/to/backup:/backup mongodb-tools mongorestore --uri="mongodb://host:27017/dbname" /backup/dbname
# Export collection to JSON / 导出集合到 JSON
docker run --rm -v /path/to/export:/export mongodb-tools mongoexport --uri="mongodb://host:27017/dbname" --collection=collection_name --out=/export/data.json
# Import JSON data / 导入 JSON 数据
docker run --rm -v /path/to/import:/import mongodb-tools mongoimport --uri="mongodb://host:27017/dbname" --collection=collection_name --file=/import/data.json
# Start an interactive mongosh session / 启动交互式 mongosh 会话
docker run --rm -it mongodb-tools mongosh "mongodb://username:password@host:27017/dbname"
# Use mongosh for database administration / 使用 mongosh 进行数据库管理
docker run --rm -it mongodb-tools mongosh "mongodb://host:27017/admin" --eval "db.createUser({user: 'admin', pwd: 'password', roles: ['root']})"
# Set MongoDB URI as environment variable / 将 MongoDB URI 设置为环境变量
docker run --rm -e MONGO_URI="mongodb://username:password@host:27017/dbname" \
-v /path/to/backup:/backup \
mongodb-tools mongodump --uri="$MONGO_URI" --out /backup
# Backup with authentication / 使用认证备份
docker run --rm -v /path/to/backup:/backup mongodb-tools \
mongodump --uri="mongodb://username:password@host:27017/dbname?authSource=admin" --out /backup
# Clone the repository / 克隆仓库
git clone <repository-url>
cd mongodb_tools
# Build the image / 构建镜像
docker build -t mongodb-tools:latest .
# Verify the build / 验证构建
docker run --rm mongodb-tools mongodump --version
MONGO_TOOLS_URL: MongoDB tools download URL / MongoDB 工具下载 URL
https://fastdl.mongodb.org/tools/db/mongodb-database-tools-ubuntu2404-x86_64-100.13.0.tgzMONGOSH_URL: MongoDB Shell download URL / MongoDB Shell 下载 URL
https://downloads.mongodb.com/compass/mongodb-mongosh_2.5.9_amd64.debDEBIAN_FRONTEND: Set to noninteractive during build / 构建期间设置为 noninteractiveFor backup and restore operations, you'll need to mount volumes:
对于备份和恢复操作,您需要挂载卷:
# Mount local directory for backups / 挂载本地目录用于备份
docker run --rm -v /local/path/to/backups:/backup mongodb-tools mongodump --uri="mongodb://host:27017/dbname" --out /backup
# Mount local directory for restore / 挂载本地目录用于恢复
docker run --rm -v /local/path/to/backups:/backup mongodb-tools mongorestore --uri="mongodb://host:27017/dbname" /backup/dbname
When connecting to MongoDB instances, ensure proper network connectivity:
连接到 MongoDB 实例时,确保正确的网络连接:
# Connect to local MongoDB / 连接到本地 MongoDB
docker run --rm --network=host mongodb-tools mongodump --uri="mongodb://localhost:27017/dbname"
# Connect to remote MongoDB / 连接到远程 MongoDB
docker run --rm mongodb-tools mongodump --uri="mongodb://remote-host:27017/dbname"
Permission denied errors / 权限被拒绝错误
docker run --user $(id -u):$(id -g) if needed / 如需要,使用 docker run --user $(id -u):$(id -g)Connection timeouts / 连接超时
Authentication failures / 认证失败
This project is licensed under the Apache License 2.0 - see the LICENSE file for details.
本项目采用 Apache License 2.0 许可证 - 详情请参阅 LICENSE 文件。
Contributions are welcome! Please feel free to submit a Pull Request.
欢迎贡献!请随时提交 Pull Request。
For issues and questions:
对于问题和疑问:
This project includes automated build and deployment workflows using GitHub Actions.
本项目包含使用 GitHub Actions 的自动化构建和部署工作流程。
DOCKERHUB_USERNAME in the workflow file / 在工作流程文件中更新 DOCKERHUB_USERNAMEFor detailed setup instructions, see .github/workflows/setup.md.
有关详细设置说明,请参阅 .github/workflows/setup.md。
docker-build-push.yml - Main CI/CD pipeline / 主要 CI/CD 流水线ci.yml - Continuous integration tests / 持续集成测试For more information about the workflows, see .github/workflows/README.md.
有关工作流程的更多信息,请参阅 .github/workflows/README.md。
Content type
Image
Digest
sha256:4ac2d1159…
Size
163.3 MB
Last updated
12 months ago
docker pull wicos/mongodb-tools