Dokcer容器安装MongoDB
文章目录
在CentOS7上用Dokcer容器安装MongoDB
检查配置
请参考此文安装Docker CE
MongoDB
搜索镜像
STARS
用户最欢程度,OFFICIAL
是否为官方版1 2 3 4 5 6 7
$ sudo docker search mongodb NAME DESCRIPTION STARS OFFICIAL AUTOMATED mongo MongoDB document databases provide high avai… 5729 [OK] mongo-express Web-based MongoDB admin interface, written w… 414 [OK] tutum/mongodb MongoDB Docker image – listens in port 27017… 224 [OK] .....
拉取镜像
拉取
STARS
最多且OFFICIAL
为[OK]
(官方版)的版本即可1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
$ sudo docker pull mongo:4 Using default tag: latest latest: Pulling from library/mongo 34667c7e4631: Pull complete d18d76a881a4: Pull complete 119c7358fbfc: Pull complete 2aaf13f3eff0: Pull complete f7833eaffdda: Pull complete 8287cb5b9daf: Pull complete ea00040a145a: Pull complete eeb70119a2ba: Pull complete 066b6bd644f8: Pull complete 46317dcef8aa: Pull complete e00b1e4390cb: Pull complete c8b03bd3372c: Pull complete 884df10f0511: Pull complete Digest: sha256:69b5754ff6e859b2edb478112d4f091f23c6c0e73d869a80037de3ce587f3dff Status: Downloaded newer image for mongo:latest
查看拉取到本地的MongoDB镜像
1 2 3 4 5
$ sudo docker images REPOSITORY TAG IMAGE ID CREATED SIZE mongo latest 4456f3d84674 2 days ago 409MB ......
创建容器
推荐创建容器时,始终指定镜像的版本——Image:Tag
简单模式
|
|
配置模式
创建本地目录
1 2 3 4 5 6 7
# 用于存放数据 $ mkdir -p ~/soft/mongodb/data # 用于存储配置文件 $ mkdir -p ~/soft/mongodb/etc # 合成一句 $ mkdir -p ~/soft/mongodb/data ~/soft/mongodb/etc
创建配置文件,详细配置项
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
# mongod.conf # for all options, see: # http://docs.mongodb.org/manual/reference/configuration-options/ # Where and how to store data. storage: dbPath: /data/db/ journal: enabled: true # engine: # mmapv1: # wiredTiger: # where to write logging data. # 如果配置了systemLog,则docker不再收集 # docker的容器日志只收集容器输出到STDOUT/STDERR的日志 # 一旦容器在内部指定了文件收集,docker就不再收集了 # 希望docker收集日志,那就将systemLog及其子项全部注释掉 # https://github.com/docker-library/mongo/issues/103#issuecomment-233438299 systemLog: destination: file logAppend: true path: /var/log/mongodb/mongod.log # network interfaces net: port: 27017 # Listen to local interface only. # Comment out to listen on all interface. # bindIp: 127.0.0.1,10.1.2.7 # 监听所有请求 bindIpAll: true # how the process runs processManagement: timeZoneInfo: /usr/share/zoneinfo #security: #operationProfiling: #replication: #sharding: ## Enterprise-Only Options: #auditLog: #snmp:
创建MongoDB容器
1 2 3 4
$ sudo docker run --name imongo -p 27017:27017 \ -v ~/soft/mongodb/data:/data/db \ -v ~/soft/mongodb/etc/mongod.conf:/etc/mongod.conf \ -d mongo:4 --config /etc/mongod.conf
注意:
- --config /etc/mongod.conf
是作用在imongo
容器内的mongod
上的。
- /etc/mongod.conf
是容器imongo
内部路径。
进入Shell
- 进入MongoDB Shell
- https://docs.mongodb.com/manual/mongo/
https://stackoverflow.com/questions/32944729/how-to-start-a-mongodb-shell-in-docker-container
1 2 3 4 5 6 7 8 9 10 11 12 13
# docker exec --help # <CONTAINER-NAME> 进入imongo Bash $ sudo docker exec -it imongo bash # 进入MongoDB Shell,在imongo容器内执行 $ mongo # 上方两个命令可以连起来写 $ sudo docker exec -it imongo mongo # 退出MongoDB Shell $ exit #imongo Bash $ exit
停止容器
Docker容器停止mongodb数据库
1 2 3 4 5 6 7 8 9 10 11
# 查看运行中的容器 $ sudo docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d99227de97dd mongo:4 "docker-entrypoint.s…" 9 seconds ago Up 7 seconds 27017/tcp imongo # <CONTAINER ID> 根据容器ID停止容器 $ sudo docker stop d99227de97dd # <CONTAINER NAMES> 根据容器名停止容器 $ sudo docker stop imongo
MongoDB Express
基于Web浏览器的UI组件,用户可以通过浏览器来访问MongoDB
拉取镜像
|
|
创建容器
需要先启动MongoDB,再用
link
命令将其链接到MongoDB,表示二者可通信1
$ sudo docker run --name imexp --link imongo:mongo -p 9091:8081 -d mongo-express
在浏览器中访问
http://localhost:9091
即可
链接
- https://docs.docker.com/samples/library/mongo/
- https://docs.docker.com/samples/library/mongo-express/
- https://docs.mongodb.com/manual/reference/configuration-options/
- https://blog.csdn.net/qq_40775879/article/details/80965650
- https://shift-alt-ctrl.iteye.com/blog/2242907
- https://yq.aliyun.com/articles/691383
- https://brickyang.github.io/2017/03/15/%E5%88%A9%E7%94%A8-Docker-%E8%BF%90%E8%A1%8C-MongoDB/