1

Using - Docker version 20.10.7, build f0df350

This is what I want to do: Have two containers in same docker network - one each for MongoDb and Mongo-Express. Expose localhost port 8090 to container port 8081 for Mongo-Express. Not exposing port in the MongoDd container.

Images pulled as docker pull mongo, and docker pull mongo-express

Scenario 1: works fine

Using the run statements as below, I can browse localhost:8090 and see Mongo-Express.

rohit@rohitu2004lts:~$ docker run -d -e --MONGO_INITDB_ROOT_USERNAME=admin -e --MONGO_INITDB_ROOT_PASSWORD=password --name mongo --net testMongoNw mongo
7b1c0ca1518d0d8390efa384fcd76504295e861547e1644cb01f1862624018af
rohit@rohitu2004lts:~$ docker run -d -p 8090:8081 -e --ME_CONFIG_MONGODB_ADMINUSERNAME=admin -e --ME_CONFIG_MONGODB_ADMINPASSWORD=password -e --ME_CONFIG_MONGODB_SERVER=mongo --name myMongoExpress --net testMongoNw mongo-express
f7ab70c43fba1dd23e2e78ae3ddf08cce28fd75d0459938b1c6f057a564decc2
rohit@rohitu2004lts:~$ docker ps -a
CONTAINER ID   IMAGE           COMMAND                  CREATED          STATUS          PORTS                                       NAMES
f7ab70c43fba   mongo-express   "tini -- /docker-ent…"   7 seconds ago    Up 4 seconds    0.0.0.0:8090->8081/tcp, :::8090->8081/tcp   myMongoExpress
7b1c0ca1518d   mongo           "docker-entrypoint.s…"   17 seconds ago   Up 14 seconds   27017/tcp                                   mongo
rohit@rohitu2004lts:~$ docker ps -a
CONTAINER ID   IMAGE           COMMAND                  CREATED          STATUS          PORTS                                       NAMES
f7ab70c43fba   mongo-express   "tini -- /docker-ent…"   19 seconds ago   Up 16 seconds   0.0.0.0:8090->8081/tcp, :::8090->8081/tcp   myMongoExpress
7b1c0ca1518d   mongo           "docker-entrypoint.s…"   29 seconds ago   Up 27 seconds   27017/tcp                                   mongo
rohit@rohitu2004lts:~$ docker logs f7ab
Welcome to mongo-express
------------------------


(node:8) [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.
Mongo Express server listening at http://0.0.0.0:8081
Server is open to allow connections from anyone (0.0.0.0)
basicAuth credentials are "admin:pass", it is recommended you change this in your config.js!
rohit@rohitu2004lts:~$ 

Snapshot of browsing localhost:8090

Scenario 2: not working

But if I only change the container name while starting MongoDb from "mongo" to "mymongo" (or any other name), the Mongo-Express starts but then exits after a few seconds.

rohit@rohitu2004lts:~$ docker run -d -e --MONGO_INITDB_ROOT_USERNAME=admin -e --MONGO_INITDB_ROOT_PASSWORD=password --name mymongo --net testMongoNw mongo
875b4e6b470fca677bd0566a586a40976d5f12b6e71c8d924c8889b07e9e7766
rohit@rohitu2004lts:~$ docker run -d -p 8090:8081 -e --ME_CONFIG_MONGODB_ADMINUSERNAME=admin -e --ME_CONFIG_MONGODB_ADMINPASSWORD=password -e --ME_CONFIG_MONGODB_SERVER=mymongo --name myMongoExpress --net testMongoNw mongo-express
a5e0da7e1b83e018a7010ebdb4e430d4b186161a9ad1bc36c8ce278027e705d0
rohit@rohitu2004lts:~$ docker ps -a
CONTAINER ID   IMAGE           COMMAND                  CREATED          STATUS          PORTS                                       NAMES
a5e0da7e1b83   mongo-express   "tini -- /docker-ent…"   6 seconds ago    Up 4 seconds    0.0.0.0:8090->8081/tcp, :::8090->8081/tcp   myMongoExpress
875b4e6b470f   mongo           "docker-entrypoint.s…"   34 seconds ago   Up 32 seconds   27017/tcp                                   mymongo
rohit@rohitu2004lts:~$ docker ps -a
CONTAINER ID   IMAGE           COMMAND                  CREATED          STATUS                     PORTS       NAMES
a5e0da7e1b83   mongo-express   "tini -- /docker-ent…"   16 seconds ago   Exited (0) 8 seconds ago               myMongoExpress
875b4e6b470f   mongo           "docker-entrypoint.s…"   44 seconds ago   Up 41 seconds              27017/tcp   mymongo
rohit@rohitu2004lts:~$ docker logs a5e
Welcome to mongo-express
------------------------


(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.
Could not connect to database using connectionString: mongodb://mongo:27017"
(node:7) UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [mongo:27017] on first connect [Error: getaddrinfo EAI_AGAIN mongo
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:66:26) {
  name: 'MongoNetworkError'
}]
    at Pool.<anonymous> (/node_modules/mongodb/lib/core/topologies/server.js:441:11)
    at Pool.emit (events.js:314:20)
    at /node_modules/mongodb/lib/core/connection/pool.js:564:14
    at /node_modules/mongodb/lib/core/connection/pool.js:1000:11
    at /node_modules/mongodb/lib/core/connection/connect.js:32:7
    at callback (/node_modules/mongodb/lib/core/connection/connect.js:289:5)
    at Socket.<anonymous> (/node_modules/mongodb/lib/core/connection/connect.js:319:7)
    at Object.onceWrapper (events.js:421:26)
    at Socket.emit (events.js:314:20)
    at emitErrorNT (internal/streams/destroy.js:92:8)
    at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
    at processTicksAndRejections (internal/process/task_queues.js:84:21)
(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)
(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.
rohit@rohitu2004lts:~$ 

In the parameter value for ME_CONFIG_MONGODB_SERVER in the Mongo-Express run command, if I correctly specify the MongoDB container name then it should have worked right...what am doing wrong in scenario 2? Thank you.

rbewoor
  • 305
  • 1
  • 3
  • 14

1 Answers1

0

The problem is with the connection string. When you change the container name to mymongo, this also gets passed as the value for ME_CONFIG_MONGODB_SERVER to mymongo. This value is what determines the host parameter value in the string. Hence change your connection string to: mongodb://mymongo:27017

Tyler2P
  • 2,324
  • 26
  • 22
  • 31