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 0.0.0.0 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 folowingenv 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!

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

Thanks

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.