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

时间:2015-12-12 11:24:38

标签: mongodb docker

我想创建一个配置了客户端访问控制的mongodb的docker容器(用户身份验证,请参阅this)。

我已使用this image使用mongo成功配置了一个docker容器。但它不使用mongo访问控制。

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

使用标准的mongodb安装我通常会执行以下步骤:

  • 在没有--auth
  • 的情况下运行mongod
  • 连接到mongo并添加管理员用户
  • 使用--auth
  • 重新启动mongo

我应该如何使用码头工具?因为mongo图像始终没有--auth。我应该创建一个新图像吗?或者修改入口点?

可能我错过了一些东西,我是码头工人的新手......

2 个答案:

答案 0 :(得分:7)

好的,我找到了解决方案。基本上MongoDb具有允许设置访问安全性(--auth)但允许本地主机连接的功能。 请参阅mongo local 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"

答案 1 :(得分:1)

如果您能够使用其他现有图像,则可以使用维护良好的图像,并为MongoDB启用默认身份验证并且易于插入,称为tutum-docker-mongodb

它还使用您可以在app中使用的环境变量。

我将其添加到我的tutum.yml(或docker-compose.yml)中,如此:

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

最后,我使用以下方式链接了网络服务:

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

希望它有所帮助!