How to connect mongodb on linux-host and mongo-express in docker

431 Views Asked by At

I have installed mongodb successfully on a linux-host (redhat 8). mongosh is working perfectly and mongod listens on port 27017. I'd like to install mongo-express as docker container and connect it to mongod running on the host-machine (outside docker!). I have tried the following docker-compose.yml:

version: '3.7'

services:

    mongo-express:
        image: mongo-express
        restart: always
       # ports:
       #     - 8081:8081
        environment:
            ME_CONFIG_MONGODB_SERVER: localhost
            ME_CONFIG_MONGODB_ADMINUSERNAME: ${MONGO_USER}
            ME_CONFIG_MONGODB_ADMINPASSWORD: ${MONGO_PASS}
            ME_CONFIG_BASICAUTH_USERNAME: ${MONGO_USER}
            ME_CONFIG_BASICAUTH_PASSWORD: ${MONGO_PASS}
        network_mode: host

I commented out the port-settings as incompatible with network_mode:host.

Result: The container will be built and gives this warnings:

mongoexpress-mongo-express-1  | Welcome to mongo-express
mongoexpress-mongo-express-1  | ------------------------
mongoexpress-mongo-express-1  |
mongoexpress-mongo-express-1  |
mongoexpress-mongo-express-1  | (node:7) [MONGODB DRIVER] Warning: Current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
mongoexpress-mongo-express-1  | (node:7) UnhandledPromiseRejectionWarning: MongoError: not authorized on config to execute command { listCollections: 1, filter: {}, cursor: {}, nameOnly: false, lsid: { id: UUID("2e2c2b92-4d5d-47d0-9c37-59e52dd3a5df") }, $db: "config" }
mongoexpress-mongo-express-1  |     at Connection.<anonymous> (/node_modules/mongodb/lib/core/connection/pool.js:453:61)
mongoexpress-mongo-express-1  |     at Connection.emit (events.js:314:20)
mongoexpress-mongo-express-1  |     at processMessage (/node_modules/mongodb/lib/core/connection/connection.js:456:10)
mongoexpress-mongo-express-1  |     at Socket.<anonymous> (/node_modules/mongodb/lib/core/connection/connection.js:625:15)
mongoexpress-mongo-express-1  |     at Socket.emit (events.js:314:20)
mongoexpress-mongo-express-1  |     at addChunk (_stream_readable.js:297:12)
mongoexpress-mongo-express-1  |     at readableAddChunk (_stream_readable.js:272:9)
mongoexpress-mongo-express-1  |     at Socket.Readable.push (_stream_readable.js:213:10)
mongoexpress-mongo-express-1  |     at TCP.onStreamRead (internal/stream_base_commons.js:188:23)
mongoexpress-mongo-express-1  | (node:7) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
mongoexpress-mongo-express-1  | (node:7) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Trying to access mongo-express on host gives:

[root@d20p280a mongoexpress]# curl localhost:8081
curl: (7) Failed to connect to localhost port 8081: Connection refused

docker ps:

CONTAINER ID   IMAGE               COMMAND                  CREATED          STATUS                PORTS                                              NAMES
5cc00d871e82   mongo-express       "tini -- /docker-ent…"   13 minutes ago   Up 13 minutes                                                            mongoexpress-mongo-express-1

Any hint appreciated - thanks!

1

There are 1 best solutions below

0
On

The devil lies in the details, as always. The missing clue was this entry in the error log:

mongoexpress-mongo-express-1  | (node:7) UnhandledPromiseRejectionWarning: MongoError: not authorized on config to execute command { listCollections: 1, filter: {}, cursor: {}, nameOnly: false, lsid: { id: UUID("2e2c2b92-4d5d-47d0-9c37-59e52dd3a5df") }, $db: "config" }

The docker-compose.yml is correct as shown. The admin user in mongodb has only the following roles: "userAdminAnyDatabase", "dbAdminAnyDatabase", "readWriteAnyDatabase" which are missing the listCollections privilege. Strange though, that mongo-express didn't even start the server. I would have expected an error message in the webbrowser. Thanks to all who took time to examine the problem. This is the admin role in use now:

roles: [
    {
        role: 'root',
        db: 'admin'
    }
]