Docker中安装并使用MongoDB
发布于 2018-06-04    260 次阅读
Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化。容器是完全使用沙箱机制,相互之间不会有任何接口。MongoDB是一个基于分布式文件存储的数据库。由C++语言编写。旨在为WEB应用提供可扩展的高性能数据存储解决方案。本文主要介绍了,如何在Docker中安装并使用MongoDB

在Docker中查询可用的MongoDB镜像

安装命令如下: 

docker search mongo

数据输出信息如下,OFFICIAL标签标记为[OK]的,说明是官方镜像: 

voidcc.com:~ develong$ docker search mongo
NAME                          DESCRIPTION                                     STARS  OFFICIAL  AUTOMATED
mongo                         MongoDB document databases provide high avai…   4523   [OK]
mongo-express                 Web-based MongoDB admin interface, written w…   254    [OK]
tutum/mongodb                 MongoDB Docker image – listens in port 27017…   224              [OK]
mvertes/alpine-mongo          light MongoDB container                         75               [OK]
mongoclient/mongoclient       Official docker image for Mongoclient, featu…   50               [OK]
bitnami/mongodb               Bitnami MongoDB Docker Image                    33               [OK]
frodenas/mongodb              A Docker Image for MongoDB                      17               [OK]
mongooseim/mongooseim         Small docker image for MongooseIM - robust a…   15
mongooseim/mongooseim-docker  MongooseIM server the latest stable version     11               [OK]
istepanov/mongodump           Docker image with mongodump running as a cro…   5                [OK]
centos/mongodb-26-centos7     MongoDB NoSQL database server                   5
khezen/mongo                  MongoDB Docker image supporting RocksDB stor…   4                [OK]
eses/mongodb_exporter         mongodb exporter for prometheus                 4                [OK]
cvallance/mongo-k8s-sidecar   Kubernetes side car to setup and maintain a …   4                [OK]
centos/mongodb-32-centos7     MongoDB NoSQL database server                   4
neowaylabs/mongodb-mms-agent  This Docker image with MongoDB Monitoring Ag…   2                [OK]
19hz/mongo-container          Mongodb replicaset for coreos                   1                [OK]
openshift/mongodb-24-centos7  DEPRECATED: A Centos7 based MongoDB v2.4 ima…   1
circleci/mongo                MongoDB document databases provide high avai…   1
ekesken/mongo                 docker image for mongo that is configurable …   1                [OK]
extremeprog/mongo-sites-api   mongo-sites-api automated build                 1                [OK]
centos/mongodb-34-centos7     MongoDB NoSQL database server                   0
xogroup/mongodb_backup_gdrive Docker image to create a MongoDB database ba…   0                [OK]
quilt/mongo                   MongoDB container for quilt.io                  0                [OK]

下载最新的的官方镜像

MongoDB最新的版本是3.6,下载命令如下:

docker pull mongo:3.6

日志输出信息如下: 

voidcc.com:~ develong$ docker pull mongo:3.6
3.6: Pulling from library/mongo
4d0d76e05f3c: Pull complete
2da2ecd7fdbd: Pull complete
c3a86da34d0f: Pull complete
e2b1f447e420: Pull complete
c9e820834b36: Pull complete
ffa34fa64bf4: Pull complete
45ba5a2a5216: Pull complete
6024791f7c87: Pull complete
796e396ed72c: Pull complete
a4ac33c0a702: Pull complete
Digest: sha256:89672f72dde6ec88618fcfa2fd11a45d147cbd7df97573c227ff8d80131c3c53
Status: Downloaded newer image for mongo:3.6

运行MongoDB镜像

运行命令如下:

docker run -p 27017:27017 -v /develop/mongo/data:/data/db -d mongo:3.6

操作日志输出如下:

voidcc.com:~ develong$ docker run -p 27017:27017 -v /develop/mongo/data:/data/db -d mongo:3.6
9719dd383d5575a6cac821aeec11fa1b3ab1501e80de1ebc840343b2f8df5ee9

命令说明

-p 27017:27017 : 将容器的27017 端口映射到主机的27017 端口

-v /develop/mongo/data:/data/db :将本机的/develop/mongo/data目录挂载到容器的/data/db,作为mongo数据存储目录

通过docker ps命令可以查看到我们刚刚Mongo的运行信息信息,容器ID为9719dd383d55

voidcc.com:~ develong$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                      NAMES
9719dd383d55        mongo:3.6           "docker-entrypoint.s…"   32 seconds ago      Up 33 seconds       0.0.0.0:27017->27017/tcp   practical_ride

MongoDB使用

命令行使用方式

通过docker exec命令进入到容器中,其中容器ID通过docker ps命令查看

voidcc.com:~ develong$ docker exec -it 9719dd383d55 /bin/bash

通过mongo命令连接到MongoDB中,接下来就可以通过MongoDB的命令来操作了

root@9719dd383d55:/# mongo
MongoDB shell version v3.6.5
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.6.5
Server has startup warnings:
2018-06-04T01:14:10.548+0000 I CONTROL  [initandlisten]
2018-06-04T01:14:10.548+0000 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-06-04T01:14:10.548+0000 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2018-06-04T01:14:10.548+0000 I CONTROL  [initandlisten]
>

注 : 默认mongodb是不使用用户认证

版权说明 : 本文为 开发小院 原创文章, 转载时请注明作者和原文连接

文章标题 : Docker中安装并使用MongoDB

文章连接 : http://www.voidcc.com/content/install-and-use-mongodb-in-docker


 主题目录

    欢迎关注本站公众号 开发小院