使用docker-compose

时间:2018-01-18 15:06:00

标签: mongodb authentication docker docker-compose

如何使用docker-composeMongoDB启用身份验证? 这是我的docker-compose.yml

version: "2"

services:
  mongo-db:
    image: "mongo:3.6.0"
    container_name: mongo-db
    ports:
      - 27017:27017
    environment:
      - MONGO_INITDB_ROOT_USERNAME=backend
      - MONGO_INITDB_ROOT_PASSWORD=123456
      - MONGO_INITDB_DATABASE=backend
    command: mongod --bind_ip 0.0.0.0

  backend:
    build:
      context: .
    container_name: backend
    ports:
      - 3030-3039:3030-3039
    depends_on:
      - mongo-db

我不使用身份验证时效果很好。

但是当我启用身份验证时会发生错误:

mongo-db        | 2018-01-18T14:52:36.836+0000 I NETWORK  [conn4] received client metadata from 172.27.0.3:49742 conn: { driver: { name: "nodejs", version: "2.2.34" }, os: { type: "Linux", name: "linux", architecture: "x64", version: "4.4.0-109-generic" }, platform: "Node.js v9.3.0, LE, mongodb-core: 2.1.18" }
mongo-db        | 2018-01-18T14:52:36.866+0000 I ACCESS   [conn4] SCRAM-SHA-1 authentication failed for backend on backend from client 172.27.0.3:49742 ; UserNotFound: Could not find user backend@backend

StackOverflow和互联网上有很多解决方案,但没有一个可行。这张图片有什么变化吗?我可以在不建立自己的形象的情况下这样做吗?

1 个答案:

答案 0 :(得分:0)

I had build a new image from mongo我配置数据库和用户/密码。

现在我们可以使用:

进行配置
services:
  mongo-db:
    image: vepo/mongo
    container_name: mongo-db
    ports:
      - 27017:27017
    environment:
      - AUTH=yes
      - ADMIN_USER=admin
      - ADMIN_PASS=admin
      - APPLICATION_DATABASE=backend
      - APPLICATION_USER=backend
      - APPLICATION_PASS=XXXXXXXXXXX