Local system's Mongo authentication in Docker

I’m trying to use MongoDB for the first time for a Sails.js app running on docker using Docker Desktop on Windows.

Please note, I’m connecting my LOCAL SYSTEM’S MongoDB (not a Mongo container) to my app’s docker container.

For test purposes, I used to bind all IPs in my mongod.cfg and I used host.docker.internal as the hostname for docker to connect to my system’s MongoDB.

Dockerfile has the following env parameters:

ENV PORT=1337 DB_HOSTNAME=host.docker.internal DB_USERNAME=root DB_PASSWORD=root DB_NAME=Test DB_PORT=27017

I used these env variables in my Sails datastore to connect to the db.

I get the following error when I run my container despite having the proper roles to the user root. root has dbOwner and readWrite rights.

MongoError: command aggregate requires authentication

    at MessageStream.messageHandler (/var/www/myapp/node_modules/mongodb/lib/cmap/connection.js:263:20)

    at MessageStream.emit (node:events:390:28)

    at processIncomingData (/var/www/myapp/node_modules/mongodb/lib/cmap/message_stream.js:144:12)

    at MessageStream._write (/var/www/myapp/node_modules/mongodb/lib/cmap/message_stream.js:42:5)

    at writeOrBuffer (node:internal/streams/writable:389:12)

    at _write (node:internal/streams/writable:330:10)

    at MessageStream.Writable.write (node:internal/streams/writable:334:10)

    at Socket.ondata (node:internal/streams/readable:754:22)

    at Socket.emit (node:events:390:28)

    at addChunk (node:internal/streams/readable:315:12)

    at readableAddChunk (node:internal/streams/readable:289:9)

    at Socket.Readable.push (node:internal/streams/readable:228:10)

    at TCP.onStreamRead (node:internal/stream_base_commons:199:23)

Emitted 'error' event on ChangeStream instance at:

    at processError (/var/www/myapp/node_modules/mongodb/lib/change_stream.js:567:38)

    at ChangeStreamCursor.<anonymous> (/var/www/myapp/node_modules/mongodb/lib/change_stream.js:436:5)

    at ChangeStreamCursor.emit (node:events:390:28)

    at /var/www/myapp/node_modules/mongodb/lib/core/cursor.js:343:16

    at /var/www/myapp/node_modules/mongodb/lib/core/cursor.js:745:9

    at /var/www/myapp/node_modules/mongodb/lib/change_stream.js:330:9

    at done (/var/www/myapp/node_modules/mongodb/lib/core/cursor.js:458:7)

    at /var/www/myapp/node_modules/mongodb/lib/core/cursor.js:542:11

    at executeCallback (/var/www/myapp/node_modules/mongodb/lib/operations/execute_operation.js:70:5)

    at callbackWithRetry (/var/www/myapp/node_modules/mongodb/lib/operations/execute_operation.js:122:14)

    at /var/www/myapp/node_modules/mongodb/lib/operations/command_v2.js:85:9

    at /var/www/myapp/node_modules/mongodb/lib/cmap/connection_pool.js:354:13

    at handleOperationResult (/var/www/myapp/node_modules/mongodb/lib/core/sdam/server.js:493:5)

    at MessageStream.messageHandler (/var/www/myapp/node_modules/mongodb/lib/cmap/connection.js:263:11)

    at MessageStream.emit (node:events:390:28)

    at processIncomingData (/var/www/myapp/node_modules/mongodb/lib/cmap/message_stream.js:144:12) {

  ok: 0,

  code: 13,

  codeName: 'Unauthorized',

  '$clusterTime': {

    clusterTime: Timestamp { _bsontype: 'Timestamp', low_: 1, high_: 1656582517 },

    signature: {

      hash: Binary {

        _bsontype: 'Binary',

        sub_type: 0,

        position: 20,

        buffer: Buffer(20) [Uint8Array] [

          219, 242,  28, 129, 253, 119,

          198, 223, 175, 105,  63, 222,

          228,   0,   1, 204,  26, 204,

           65, 185



      keyId: Long { _bsontype: 'Long', low_: 1, high_: 1645435952 }



  operationTime: Timestamp { _bsontype: 'Timestamp', low_: 1, high_: 1656582517 }


Any help would be appreciated. Thanks!

Just to let people know that it was asked on other forums as well, so they can see if it was already solved:

Since I don’t know anything about your container that want’s to access MongoDB, I can’t tell you if your variables are wrong or not. I searched for the first line of the error message and this is what I have found:

This is not the same, but similar. Here is an other with shorter explanation and less vote:

Thank you but I saw all these solutions. The problem I’m having is specifically related to Docker. When I run my sails my application outside Docker, the authentication mechanism I have works fine. It’s just not working when I try to connect my system’s MongoDB with my app’s Docker container. Thanks!

This is a good example why it is recommended in the question to share what you have already tried and found. Do you have other issues that you found and didn’t help?

It may be true, but I don’t know Sails.js, so I don’t know how it should be configured, and the error message itself doesn’t say anything about Docker.

Can you create a reproducable example that you can share?

You can also try to

  • configure an other mongodb just for debugging and see if it works when you try to connect from one container to an other.
  • use an incorrect hostname to see if the error message is different
  • use a hostname pointing to a wrong IP or just use a wrong IP instead of the hostname and see if the error message is different
  • use the current hostname, but stop the mongodb on your host to see if the error message is different
  • run a simple webserver on your windows host using python or anything you like, listen on the same IP, but different ports and see if you can access that webserver using the current hostname from a container

When we know what the problem is, we can help to solve it, but the error message only says a command requires autoentication and I have no idea why it would be a problem when you can access mongodb.

I reinstalled my docker desktop and somehow the issue was gone. Not sure what was the issue.

But thanks.