1、取最新版的 MongoDB 镜像

>docker pull mongo:latest

2、查看本地镜像

>docker images
REPOSITORY                                                      TAG                 IMAGE ID            CREATED             SIZE
mongo                                                           latest              c97feb3412a3        4 days ago          493MB

3、运行

>docker run -itd --name mongo -p 27017:27017 mongo
  • -p 27017:27017 :映射容器服务的 27017 端口到宿主机的 27017 端口。外部可以直接通过 宿主机 ip:27017 访问到 mongo 的服务。

4、查看容器的运行信息

>docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                      NAMES
ca5ea342229f        mongo               "docker-entrypoint.s…"   5 seconds ago       Up 4 seconds        0.0.0.0:27017->27017/tcp   mongo

5、连接

>docker exec -it mongo mongo
MongoDB shell version v4.4.3
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("fb94ffa2-8797-440e-86e5-e8bc567810f2") }
MongoDB server version: 4.4.3
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
	https://docs.mongodb.com/
Questions? Try the MongoDB Developer Community Forums
	https://community.mongodb.com
---
The server generated these startup warnings when booting: 
        2021-01-09T10:52:11.824+00:00: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine. See http://dochub.mongodb.org/core/prodnotes-filesystem
        2021-01-09T10:52:12.281+00:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
        2021-01-09T10:52:12.282+00:00: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never'
        2021-01-09T10:52:12.282+00:00: /sys/kernel/mm/transparent_hugepage/defrag is 'always'. We suggest setting it to 'never'
---
---
        Enable MongoDB's free cloud-based monitoring service, which will then receive and display
        metrics about your deployment (disk utilization, CPU, operation statistics, etc).

        The monitoring data will be available on a MongoDB website with a unique URL accessible to you
        and anyone you share the URL with. MongoDB may use this information to make product
        improvements and to suggest MongoDB products and deployment options to you.

        To enable free monitoring, run the following command: db.enableFreeMonitoring()
        To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---

 

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐