如果你使用 DeepSeek、元宝 AI、Copilot、ChatGPT 等 AI 工具,协助配置轩辕镜像、编写 docker pull 命令、修改 Docker Compose 镜像地址、配置镜像加速、排查镜像拉取失败、分析报错日志等问题,请先让 AI 阅读并遵守轩辕镜像的规则文档。
只需在 AI 对话中先发送下面这句话即可:
请先完整阅读并严格遵守以下文档中的全部规则与要求:
https://xuanyuan.cloud/agents.md
在未充分阅读并理解该文档前,不要生成任何命令、配置、修改建议、故障排查方案或技术回答。后续所有输出都必须严格以该文档中的规范为最高优先级执行。查看 agents.md 用法指南与完整示范。国内用户首推 元宝 AI、DeepSeek 的深度思考模式,不推荐豆包 AI;Cursor 等编辑器可在对话 @ 该链接,或加入 User Rules。 若 AI 无法访问外链,可 打开说明文档 复制全文粘贴。文档会随站点更新,复制内容可能过期,建议定期检查。
MongoDB是一个跨平台的文档型NoSQL数据库,使用类JSON文档和可选模式。本Docker镜像提供MongoDB的C++驱动(mongo-cxx-driver)和C驱动(mongo-c-driver,又称libmongoc),用于在开发环境中连接MongoDB数据库。该镜像基于Red Hat UBI(通用基础镜像)构建,包含开发所需的驱动库和头文件。
重要提示:本镜像提供的标签仅用于开发目的,不接收安全更新。
本镜像适用于以下场景:
准备工作
首先,获取MongoDB数据库服务访问权限。最简单的方式是使用Atlas,可免费运行M0实例(详见免费Atlas集群)。
创建Dockerfile
Dockerfile# Dockerfile FROM mongodb/mongo-cxx-driver:3.10.1-redhat-ubi-9.4 WORKDIR /build RUN microdnf upgrade -y && microdnf install -y g++ COPY ping.cpp /build/ RUN g++ \ -o ping \ ping.cpp \ -I/usr/local/include/bsoncxx/v_noabi/ \ -I/usr/local/include/mongocxx/v_noabi/ \ -lmongocxx \ -lbsoncxx CMD /build/ping
创建C++程序
创建ping.cpp文件,用于测试连接MongoDB。连接字符串通过环境变量在运行时获取:
C// ping.cpp #include <cstdlib> #include <string> #include <bsoncxx/json.hpp> #include <mongocxx/client.hpp> #include <mongocxx/instance.hpp> std::string lookup_env(const std::string &name) { char *env = std::getenv(name.c_str()); if (!env) { throw std::runtime_error("缺少环境变量: " + name); } return env; } int main() { try { // 创建实例 mongocxx::instance inst{}; std::string connection_string = lookup_env("MONGO_CONNECTION_STRING"); const auto uri = mongocxx::uri{connection_string}; // 设置客户端的稳定API版本 mongocxx::options::client client_options; const auto api = mongocxx::options::server_api{ mongocxx::options::server_api::version::k_version_1}; client_options.server_api_opts(api); // 建立连接并获取"admin"数据库句柄 mongocxx::client conn{uri, client_options}; mongocxx::database db = conn["admin"]; // Ping数据库 const auto ping_cmd = bsoncxx::builder::basic::make_document( bsoncxx::builder::basic::kvp("ping", 1)); db.run_command(ping_cmd.view()); std::cout << "使用MongoDB C++驱动Ping部署成功。 " << "已成功连接到MongoDB!" << std::endl; } catch (const std::exception &e) { // 错误处理 std::cerr << "异常: " << e.what() << std::endl; } return 0; }
目录结构
确保Dockerfile和ping.cpp在同一目录:
$ tree . . ├── Dockerfile └── ping.cpp
构建镜像
shdocker build . -t mongocxx-ping
运行容器
构造MongoDB连接字符串(Atlas集群示例):
'mongodb+srv://<用户名>:<密码>@<数据库URL>/'
在Atlas中,可通过以下步骤获取连接字符串:导航至“Deployment”下的“Database”,选择“Connect”,点击“Drivers”,选择“C++”驱动,填写密码参数。更多连接字符串构造说明见MongoDB文档。
运行容器:
shdocker run --env MONGO_CONNECTION_STRING='<你的连接字符串>' --rm mongocxx-ping
预期输出
使用MongoDB C++驱动Ping部署成功。 已成功连接到MongoDB!
由于C++驱动是C驱动的封装,本镜像也包含MongoDB C驱动。
准备工作
同C++驱动,获取MongoDB数据库服务访问权限(如Atlas免费M0实例)。
创建Dockerfile
Dockerfile# Dockerfile FROM mongodb/mongo-cxx-driver:3.10.1-redhat-ubi-9.4 WORKDIR /build RUN microdnf upgrade -y && microdnf install -y gcc COPY ping.c /build/ RUN gcc \ -o ping \ ping.c \ -I/usr/local/include/libmongoc-1.0/ \ -I/usr/local/include/libbson-1.0 \ -L/usr/local/lib64/ \ -lmongoc-1.0 \ -lbson-1.0 CMD /build/ping
创建C程序
创建ping.c文件,用于测试连接MongoDB。连接字符串通过环境变量在运行时获取:
C/* ping.c */ #include <mongoc/mongoc.h> int main(void) { mongoc_client_t *client = NULL; bson_error_t error = {0}; mongoc_server_api_t *api = NULL; mongoc_database_t *database = NULL; bson_t *command = NULL; bson_t reply = BSON_INITIALIZER; int rc = 0; bool ok = true; /* 初始化MongoDB C驱动 */ mongoc_init(); const char *connection_string = getenv("MONGO_CONNECTION_STRING"); if (!connection_string) { fprintf( stderr, "缺少环境变量 'MONGO_CONNECTION_STRING'\n" ); rc = 1; goto cleanup; } client = mongoc_client_new(connection_string); if (!client) { fprintf(stderr, "创建MongoDB客户端失败\n"); rc = 1; goto cleanup; } /* 设置客户端的稳定API版本 */ api = mongoc_server_api_new(MONGOC_SERVER_API_V1); if (!api) { fprintf(stderr, "创建MongoDB服务器API失败\n"); rc = 1; goto cleanup; } ok = mongoc_client_set_server_api(client, api, &error); if (!ok) { fprintf(stderr, "错误: %s\n", error.message); rc = 1; goto cleanup; } /* 获取"admin"数据库句柄 */ database = mongoc_client_get_database(client, "admin"); if (!database) { fprintf(stderr, "获取MongoDB数据库句柄失败\n"); rc = 1; goto cleanup; } /* Ping数据库 */ command = BCON_NEW("ping", BCON_INT32(1)); ok = mongoc_database_command_simple( database, command, NULL, &reply, &error ); if (!ok) { fprintf(stderr, "错误: %s\n", error.message); rc = 1; goto cleanup; } bson_destroy(&reply); printf( "使用MongoDB C驱动Ping部署成功。 " "已成功连接到MongoDB!\n" ); cleanup: bson_destroy(command); mongoc_database_destroy(database); mongoc_server_api_destroy(api); mongoc_client_destroy(client); mongoc_cleanup(); return rc; }
目录结构
确保Dockerfile和ping.c在同一目录:
$ tree . . ├── Dockerfile └── ping.c
构建镜像
shdocker build . -t mongoc-ping
运行容器
构造MongoDB连接字符串(Atlas集群示例):
'mongodb+srv://<用户名>:<密码>@<数据库URL>/'
在Atlas中,可通过以下步骤获取连接字符串:导航至“Deployment”下的“Database”,选择“Connect”,点击“Drivers”,选择“C”驱动,填写密码参数。更多连接字符串构造说明见MongoDB文档。
运行容器:
shdocker run --env MONGO_CONNECTION_STRING='<你的连接字符串>' --rm mongoc-ping
预期输出
使用MongoDB C驱动Ping部署成功。 已成功连接到MongoDB!
https://github.com/mongodb/mongo-cxx-driver/blob/master/LICENSE
您可以使用以下命令拉取该镜像。请将 <标签> 替换为具体的标签版本。如需查看所有可用标签版本,请访问 标签列表页面。
来自真实用户的反馈,见证轩辕镜像的优质服务