Dennis Winter
Dennis Winter

Reputation: 73

Connect MongoDB container with nodejs app

I have two docker container one with my nodejs webapp and one with a mongo db running. I build a authentication in my mongo db container and now i get this error message:

    mongo    | 2018-08-22T13:48:24.102+0000 I ACCESS   [conn1] SASL SCRAM-SHA-1 authentication failed for blub on admin from client XXXXX; UserNotFound: Could not find user blub@admin
    mongo    | 2018-08-22T13:48:24.105+0000 I NETWORK  [conn1] end connection XXXXX (0 connections now open)
 app      | (node:16) UnhandledPromiseRejectionWarning: MongoError: Authentication failed.

I build both container with this docker-compose.yml

version: "2"
services:
 app:
   container_name: app
   restart: always
   build: .
   ports:
     - "3000:3000"
   links:
     - mongo
 mongo:
   container_name: mongo
   image: mongo
   volumes:
     - ./data:/data/db
   ports:
     - "27017:27017"
   environment:

     - MONGODB_INITDB_ROOT_USERNAME=blub
     - MONGODB_INITDB_ROOT_PASSWORD=pass

My app connects with the mongodb like this:

mongoose.connect("mongodb://blub:pass@mongo:27017/");

Upvotes: 1

Views: 1084

Answers (1)

Dennis Winter
Dennis Winter

Reputation: 73

I found a solution: My docker-compose:

version: '2'
services:
 app:
   container_name: app
   restart: always
   build: .
   ports:
     - 3000:3000
   links:
     - mongodb
 mongodb:
   container_name: mongodb
   image: mongo:3.4
   ports:
     - 27017:27017
   volumes:
      - ./data:/data/db
   environment:
     - MONGO_INITDB_ROOT_USERNAME=admin
     - MONGO_INITDB_ROOT_PASSWORD=admin123456
   command: mongod

the connection in the nodejs app: mongoose.connect("mongodb://admin:admin123456@mongodb:27017/");

But i actually can´t figure out, why it isn't working on my first try.

Upvotes: 1

Related Questions