1

I have created a node app using MongoDB as a database. I created containers for both of them and created a docker-compose file. But while connecting MongoDB with my node app I am getting an Authentication failed error. I want to connect my node container with the MongoDB container using docker-compose. I have set all the credentials but I am getting an Authentication failed error.

error:

api        |     MongoError: Authentication failed.
api        |     at MessageStream.messageHandler (/home/app/node_modules/mongodb/lib/cmap/connection.js:272:20)
api        |     at MessageStream.emit (events.js:375:28)
api        |     at processIncomingData (/home/app/node_modules/mongodb/lib/cmap/message_stream.js:144:12)
api        |     at MessageStream._write (/home/app/node_modules/mongodb/lib/cmap/message_stream.js:42:5)
api        |     at writeOrBuffer (internal/streams/writable.js:358:12)
api        |     at MessageStream.Writable.write (internal/streams/writable.js:303:10)
api        |     at Socket.ondata (internal/streams/readable.js:726:22)
api        |     at Socket.emit (events.js:375:28)
api        |     at addChunk (internal/streams/readable.js:290:12)
api        |     at readableAddChunk (internal/streams/readable.js:265:9)
api        |     at Socket.Readable.push (internal/streams/readable.js:204:10)
api        |     at TCP.onStreamRead (internal/stream_base_commons.js:188:23) {
api        |   ok: 0,
api        |   code: 18,
api        |   codeName: 'AuthenticationFailed'
api        | }

docker-compose.yml

version: "3.9"

services:
  api:
    restart: always
    build:
      context: ./server
      dockerfile: Dockerfile.dev
    container_name: api
    volumes:
      - /home/app/node_modules
      - ./server:/home/app
    ports:
      - 8000:5000
    depends_on:
      - mongodb
    environment:
      NODE_ENV: ${NODE_ENV}
      MONGO_URI: mongodb://${MONGO_ROOT_USERNAME}:${MONGO_ROOT_PASSWORD}@mongodb
    networks:
      - lib_api

  mongodb:
    image: mongo
    restart: always
    ports:
      - 27017:27017
    environment:
      MONGO_INTIDB_ROOT_USERNAME: ${MONGO_ROOT_USERNAME}
      MONGO_INTIDB_ROOT_PASSWORD: ${MONGO_ROOT_PASSWORD}
    volumes:
      - mongodb-data:/data/db
    networks:
      - lib_api

networks: 
  lib_api:
    driver: bridge

volumes: 
  mongodb-data:
    driver: local

.env

NODE_ENV=developement
MONGO_ROOT_USERNAME=niranjan
MONGO_ROOT_PASSWORD=password
DB_NAME=library

dbconfig.js

const mongoose = require("mongoose");

mongoose
    .connect(process.env.MONGO_URI, {
        dbName: process.env.DB_NAME,
        useCreateIndex: true,
        useNewUrlParser: true,
        useUnifiedTopology: true,
        useFindAndModify: false,
    })
    .then(() => console.log("database connected"))
    .catch((err) => console.log(err));

1 Answer 1

1

try this mongodb uri:

MONGODB_URI='mongodb://root:1004@<your_mongo_service_name>/<your_db_Name>?authSource=admin&w=1'
Sign up to request clarification or add additional context in comments.

1 Comment

After reading this answer I looked up this link, to confirm that I needed to set the option authSource to the correct database (admin also in my case). Thanks, debugging that took embarrassingly long.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.