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
您可以使用以下命令拉取该镜像。请将 <标签> 替换为具体的标签版本。如需查看所有可用标签版本,请访问 标签列表页面。
探索更多轩辕镜像的使用方法,找到最适合您系统的配置方式
通过 Docker 登录认证访问私有仓库
无需登录使用专属域名
Kubernetes 集群配置 Containerd
K3s 轻量级 Kubernetes 镜像加速
VS Code Dev Containers 配置
Podman 容器引擎配置
HPC 科学计算容器配置
ghcr、Quay、nvcr 等镜像仓库
Harbor Proxy Repository 对接专属域名
Portainer Registries 加速拉取
Nexus3 Docker Proxy 内网缓存
需要其他帮助?请查看我们的 常见问题Docker 镜像访问常见问题解答 或 提交工单
docker search 限制
站内搜不到镜像
离线 save/load
插件要用 plugin install
WSL 拉取慢
安全与 digest
新手拉取配置
镜像合规机制
manifest unknown
no matching manifest(架构)
invalid tar header(解压)
TLS 证书失败
DNS 超时
域名连通性排查
410 Gone 排查
402 与流量用尽
401 认证失败
429 限流
D-Bus 凭证提示
413 与超大单层
来自真实用户的反馈,见证轩辕镜像的优质服务