在本地目录上新建一个目录
mkdir -p mongo/db
# 更改更高权限
chmod -R 777 mongo
查找Docker Hub上的mongo镜像
runoob@runoob:~/mongo$ docker search mongo
INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED
docker.io docker.io/mongo MongoDB document databases provide high av... 4308 [OK]
docker.io docker.io/mongo-express Web-based MongoDB admin interface, written... 234 [OK]
docker.io docker.io/tutum/mongodb MongoDB Docker image – listens in port 270... 218 [OK]
docker.io docker.io/mvertes/alpine-mongo light MongoDB container 72 [OK]
docker.io docker.io/mongoclient/mongoclient Official docker image for Mongoclient, fea... 49 [OK]
docker.io docker.io/bitnami/mongodb Bitnami MongoDB Docker Image 31 [OK]
docker.io docker.io/frodenas/mongodb A Docker Image for MongoDB 17 [OK]
docker.io docker.io/mongooseim/mongooseim Small docker image for MongooseIM - robust... 15
docker.io docker.io/mongooseim/mongooseim-docker MongooseIM server the latest stable version 10 [OK]
docker.io docker.io/centos/mongodb-26-centos7 MongoDB NoSQL database server 5
docker.io docker.io/istepanov/mongodump Docker image with mongodump running as a c... 5 [OK]
docker.io docker.io/centos/mongodb-32-centos7 MongoDB NoSQL database server 4
docker.io docker.io/cvallance/mongo-k8s-sidecar Kubernetes side car to setup and maintain ... 4 [OK]
docker.io docker.io/eses/mongodb_exporter mongodb exporter for prometheus 4 [OK]
docker.io docker.io/khezen/mongo MongoDB Docker image supporting RocksDB st... 4 [OK]
docker.io docker.io/19hz/mongo-container Mongodb replicaset for coreos 1 [OK]
docker.io docker.io/alino/coreos-mongodb-cluster mongo 3.0 1 [OK]
docker.io docker.io/circleci/mongo MongoDB document databases provide high av... 1
docker.io docker.io/ekesken/mongo docker image for mongo that is configurabl... 1 [OK]
docker.io docker.io/extremeprog/mongo-sites-api mongo-sites-api automated build 1 [OK]
docker.io docker.io/openshift/mongodb-24-centos7 DEPRECATED: A Centos7 based MongoDB v2.4 i... 1
docker.io docker.io/ansibleplaybookbundle/mongodb-apb An APB to deploy MongoDB. 0 [OK]
docker.io docker.io/os33/go-mongo go and mongo package for testing 0
docker.io docker.io/quilt/mongo MongoDB container for quilt.io 0 [OK]
docker.io docker.io/xogroup/mongodb_backup_gdrive Docker image to create a MongoDB database ... 0 [OK]
这里我们拉取官方的镜像,标签为3.6
runoob@runoob:~/mongo$ docker pull mongo:3.6
等待下载完成后,我们就可以在本地镜像列表里查到REPOSITORY为mongo,标签为3.6的镜像。
使用mongo镜像
运行容器
runoob@runoob:~/mongo$ docker run -p 27017:27017 -v $PWD/db:/data/db -d mongo:3.6
cda8830cad5fe35e9c4aed037bbd5434b69b19bf2075c8626911e6ebb08cad51
runoob@runoob:~/mongo$
命令说明:
**-p 27017:27017 :**将容器的27017 端口映射到主机的27017 端口
**-v $PWD/db:/data/db :**将主机中当前目录下的db挂载到容器的/data/db,作为mongo数据存储目录
查看容器启动情况
runoob@runoob:~/mongo$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
575d2db35f04 mongo:3.6 "docker-entrypoint..." 18 seconds ago Up 17 seconds 0.0.0.0:27017->27017/tcp silly_sinoussi
使用mongo镜像执行mongo 命令连接到刚启动的容器,主机IP为172.17.0.1
runoob@runoob:~/mongo$ docker run -it mongo:3.6 mongo --host 10.180.0.167
MongoDB shell version v3.6.3
connecting to: mongodb://10.180.0.167:27017/
MongoDB server version: 3.6.3
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
http://docs.mongodb.org/
Questions? Try the support group
http://groups.google.com/group/mongodb-user
2018-04-04T01:24:08.695+0000 I STORAGE [main] In File::open(), ::open for '/home/mongodb/.mongorc.js' failed with No such file or directory
Server has startup warnings:
2018-04-04T01:22:06.318+0000 I CONTROL [initandlisten]
2018-04-04T01:22:06.318+0000 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-04-04T01:22:06.318+0000 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2018-04-04T01:22:06.318+0000 I CONTROL [initandlisten]
2018-04-04T01:22:06.318+0000 I CONTROL [initandlisten]
2018-04-04T01:22:06.318+0000 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2018-04-04T01:22:06.318+0000 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2018-04-04T01:22:06.318+0000 I CONTROL [initandlisten]
2018-04-04T01:22:06.318+0000 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2018-04-04T01:22:06.318+0000 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2018-04-04T01:22:06.318+0000 I CONTROL [initandlisten]
> show dbs
admin 0.000GB
local 0.000GB
>