1、拉取镜像
➜ ~ docker pull mongo
Using default tag: latest
latest: Pulling from library/mongo
f08d8e2a3ba1: Already exists
3baa9cb2483b: Already exists
94e5ff4c0b15: Already exists
1860925334f9: Already exists
9d42806c06e6: Pull complete
31a9fd218257: Pull complete
5bd6e3f73ab9: Pull complete
f6ae7a64936b: Pull complete
80fde2cb25c5: Pull complete
1bec62fe62fc: Pull complete
2cf4970a1653: Pull complete
39fac3226e16: Pull complete
86bca9c64faf: Pull complete
Digest: sha256:df9eca84736a666d5f7e7a09aeb8a6d8d073698d5b7349400f10ee75812e0e95
Status: Downloaded newer image
for mongo:latest
2、查看镜像
➜ ~ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mongo latest 409c3f937574 2 weeks ago 493MB
3、运行容器
➜ ~ docker run -itd --name mongo -p 27017:27017 mongo --auth
d49ab191cd05eeced022e522207a8f2d14d77d4562f4a3b64fff8413c0e33146
4、查看容器
➜ ~ docker
ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d49ab191cd05 mongo
"docker-entrypoint.s…" 10 minutes ago Up 10 minutes 0.0.0.0:27017-
>27017/tcp mongo
5、连接MongoDB
使用以下命令添加用户和设置密码,并且尝试连接。
➜ ~ docker
exec -it mongo mongo admin
MongoDB shell version v4.4.0
connecting to: mongodb://127.0.0.1:27017/admin?compressors
=disabled
&gssapiServiceName
=mongodb
Implicit session: session
{ "id" : UUID
("15279d24-a068-433e-9d36-d803e3250fad") }
MongoDB server version: 4.4.0
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
> db.createUser
({ user:
'admin',pwd:
'123456',roles:
[ { role:
'userAdminAnyDatabase', db:
'admin'}]});
Successfully added user:
{
"user" : "admin",
"roles" : [
{
"role" : "userAdminAnyDatabase",
"db" : "admin"
}
]
}
> db.auth
('admin',
'123456')
1