我想创建一个docker容器,其中mongodb配置了客户端访问控制(用户身份验证,请参阅此内容).
我已经使用此图像成功配置了一个带有mongo的docker容器.但它不使用mongo访问控制.
问题是要启用访问控制,我必须使用特定的命令行(--auth)运行mongodb,但仅在创建第一个管理员用户之后.
使用标准的mongodb安装,我通常会执行以下步骤:
--auth--auth我怎么用docker做这个呢?因为mongo图像总是没有开始--auth.我应该创建一个新图像吗?或者修改入口点?
可能我错过了什么,我是码头工人的新手......
好的,我找到了解决方案.基本上MongoDb具有允许设置访问安全性(--auth)但允许本地主机连接的功能.请参阅mongo本地例外.
所以这是我的最终剧本:
# Create a container from the mongo image,
# run is as a daemon (-d), expose the port 27017 (-p),
# set it to auto start (--restart)
# and with mongo authentication (--auth)
# Image used is https://hub.docker.com/_/mongo/
docker pull mongo
docker run --name YOURCONTAINERNAME --restart=always -d -p 27017:27017 mongo mongod --auth
# Using the mongo "localhost exception" add a root user
# bash into the container
sudo docker exec -i -t YOURCONTAINERNAME bash
# connect to local mongo
mongo
# create the first admin user
use admin
db.createUser({user:"foouser",pwd:"foopwd",roles:[{role:"root",db:"admin"}]})
# exit the mongo shell
exit
# exit the container
exit
# now you can connect with the admin user (from any mongo client >=3 )
# remember to use --authenticationDatabase "admin"
mongo -u "foouser" -p "foopwd" YOURHOSTIP --authenticationDatabase "admin"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2658 次 |
| 最近记录: |