有客户端访问控制的Mongodb docker容器

我想创build一个configuration了客户端访问控制(用户身份validation,请参阅此 )mongodbdocker容器。

我已经使用这个图像成功地configuration了一个与mongo的docker集装箱。 但是它不使用mongo访问控制。

问题是要启用访问控制,我必须使用特定的命令行( --auth )运行mongodb,但只能在创build第一个pipe理用户之后运行。

对于标准的mongodb安装,我通常执行以下步骤:

  • 运行mongod没有--auth
  • 连接到mongo并添加pipe理员用户
  • --auth重启mongo

我应该怎么做与docker? 因为mongo图像始终没有--auth 。 我应该创build一个新的图像? 或者也许修改入口点?

可能我错过了一些东西,我是docker工人。

好的,我find了一个解决scheme。 基本上MongoDb有一个function,可以设置访问安全( --auth ),但允许本地主机连接。 看到mongo本地exception 。

所以这是我最后的脚本:

 # 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" 

如果你能够使用其他现有的图像,那么有一个维护良好的图像,为MongoDB启用默认身份validation,并且易于插入,称为tutum-docker-mongodb

它也使用你可以在你的应用程序中使用的环境variables。

我将它包含在我的tutum.yml (或docker-compose.yml )中,如下所示:

 mongo: image: 'tutum/mongodb:latest' environment: - MONGODB_PASS=<your-password-here> ports: - '27017:27017' - '28017:28017' 

最后我使用以下方式链接了Web服务:

 web: image: 'my-image' links: - 'mongo:mongo' ports: - '80:3000' restart: always 

希望能帮助到你!