docker-compose service name problem using mongo official image

I followed official mongo image description trying to using it with docker-compose.But i’ve noticed that the mongo service name can not be the same as the image name.Here is the situation:

  1. created a docker-compose.yml file which filled with this:
version: '3.1'

services:

  mongo:
    image: mongo
    restart: always
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: example

  mongo-express:
    image: mongo-express
    restart: always
    ports:
      - 8081:8081
    environment:
      ME_CONFIG_MONGODB_ADMINUSERNAME: root
      ME_CONFIG_MONGODB_ADMINPASSWORD: example
      ME_CONFIG_MONGODB_URL: mongodb://root:example@mongo:27017/

  1. execute docker-compose
docker-compose -f stack.yml up

Then the error appears:

Could not connect to database using connectionString: mongodb://root:example@mongo:27017/"
mongo-express_1  | (node:8) UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [mongo:27017] on first connect [MongoError: Authentication failed.
mongo-express_1  |     at Connection.messageHandler (/node_modules/mongodb/lib/core/connection/connection.js:364:19)
mongo-express_1  |     at Connection.emit (events.js:314:20)

But when I change the service name mongo to myMongo,it just work out!:

version: '3.1'

services:

  myMongo:
    image: mongo
    restart: always
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: example

  mongo-express:
    image: mongo-express
    restart: always
    ports:
      - 8081:8081
    environment:
      ME_CONFIG_MONGODB_ADMINUSERNAME: root
      ME_CONFIG_MONGODB_ADMINPASSWORD: example
      ME_CONFIG_MONGODB_URL: mongodb://root:example@myMongo:27017/

So, where am i wrong?

Hi :slight_smile:

I just tried it and had only 1 error, which was because mongo wasn’t completely running before express tried to connect.

Im wondering if you’re experiencing the same thing, since the error you’re getting is “Authentication failed.” and not something connection related.

Hello i also have the same problem (even when i change the mongo service name…)

(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://root:example@mongo:27017/"
(node:7) UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [mongo:27017] on first connect [Error: connect ECONNREFUSED 192.168.160.2:27017
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1144:16) {
  name: 'MongoNetworkError'
}]

I’m using the exact docker-compose example in the documentation : Docker

The issue you’re encountering is related to the hostname of the MongoDB service that you’re trying to connect to from your mongo-express service.

In the first example where you named the service mongo, the hostname mongo is already being used by the mongo image as its own hostname. This means that the mongo-express service cannot connect to the MongoDB service as it cannot resolve the hostname mongo to the correct IP address.

In the second example where you renamed the mongo service to myMongo, you’re using a different hostname that is not being used by the mongo image, so the mongo-express service can correctly resolve the hostname to the MongoDB service.

To avoid this issue, you can either rename your MongoDB service to something other than mongo or choose a different hostname to connect to the service.

Alternatively, you could define a custom network for your Docker Compose setup and ensure that both the mongo and mongo-express services are on the same network, which would allow them to communicate with each other using their service names as hostnames.


services:
  mongo:
    image: mongo
    restart: always
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: example
    networks:
      my_network:
        aliases:
          - mongo

  mongo-express:
    image: mongo-express
    restart: always
    ports:
      - 8081:8081
    environment:
      ME_CONFIG_MONGODB_ADMINUSERNAME: root
      ME_CONFIG_MONGODB_ADMINPASSWORD: example
      ME_CONFIG_MONGODB_URL: mongodb://root:example@mongo:27017/
    networks:
      my_network:

networks:
  my_network:

gives this error:
    maxSetVersion: null,

    commonWireVersion: 0,

    logicalSessionTimeoutMinutes: null

  },

  code: undefined,

  [Symbol(errorLabels)]: Set(0) {}

}

Can any please help me to resolve it

Here is the error:

mongo-express-1  | Could not connect to database using connectionString: mongodb://mongo:27017"
mongo-express-1  | /app/node_modules/mongodb/lib/sdam/topology.js:285
mongo-express-1  |                 const timeoutError = new error_1.MongoServerSelectionError(`Server selection timed out after ${serverSelectionTimeoutMS} ms`, this.description);
mongo-express-1  |                                      ^
mongo-express-1  |
mongo-express-1  | MongoServerSelectionError: getaddrinfo ENOTFOUND mongo
mongo-express-1  |     at Timeout._onTimeout (/app/node_modules/mongodb/lib/sdam/topology.js:285:38)
mongo-express-1  |     at listOnTimeout (node:internal/timers:569:17)
mongo-express-1  |     at process.processTimers (node:internal/timers:512:7) {
mongo-express-1  |   reason: TopologyDescription {
mongo-express-1  |     type: 'Unknown',
mongo-express-1  |     servers: Map(1) {
mongo-express-1  |       'mongo:27017' => ServerDescription {
mongo-express-1  |         address: 'mongo:27017',
mongo-express-1  |         type: 'Unknown',
mongo-express-1  |         hosts: [],
mongo-express-1  |         passives: [],
mongo-express-1  |         arbiters: [],
mongo-express-1  |         tags: {},
mongo-express-1  |         minWireVersion: 0,
mongo-express-1  |         maxWireVersion: 0,
mongo-express-1  |         roundTripTime: -1,
mongo-express-1  |         lastUpdateTime: 4452511,
mongo-express-1  |         lastWriteDate: 0,
mongo-express-1  |         error: MongoNetworkError: getaddrinfo ENOTFOUND mongo
mongo-express-1  |             at connectionFailureError (/app/node_modules/mongodb/lib/cmap/connect.js:387:20)
mongo-express-1  |             at Socket.<anonymous> (/app/node_modules/mongodb/lib/cmap/connect.js:310:22)
mongo-express-1  |             at Object.onceWrapper (node:events:632:26)
mongo-express-1  |             at Socket.emit (node:events:517:28)
mongo-express-1  |             at emitErrorNT (node:internal/streams/destroy:151:8)
mongo-express-1  |             at emitErrorCloseNT (node:internal/streams/destroy:116:3)
mongo-express-1  |             at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
mongo-express-1  |           cause: Error: getaddrinfo ENOTFOUND mongo
mongo-express-1  |               at GetAddrInfoReqWrap.onlookup [as oncomplete] (node:dns:107:26) {
mongo-express-1  |             errno: -3008,
mongo-express-1  |             code: 'ENOTFOUND',
mongo-express-1  |             syscall: 'getaddrinfo',
mongo-express-1  |             hostname: 'mongo'
mongo-express-1  |           },
mongo-express-1  |           [Symbol(errorLabels)]: Set(1) { 'ResetPool' }
mongo-express-1  |         },
mongo-express-1  |         topologyVersion: null,
mongo-express-1  |         setName: null,
mongo-express-1  |         setVersion: null,
mongo-express-1  |         electionId: null,
mongo-express-1  |         logicalSessionTimeoutMinutes: null,
mongo-express-1  |         primary: null,
mongo-express-1  |         me: null,
mongo-express-1  |         '$clusterTime': null
mongo-express-1  |       }
mongo-express-1  |     },
mongo-express-1  |     stale: false,
mongo-express-1  |     compatible: true,
mongo-express-1  |     heartbeatFrequencyMS: 10000,
mongo-express-1  |     localThresholdMS: 15,
mongo-express-1  |     setName: null,
mongo-express-1  |     maxElectionId: null,
mongo-express-1  |     maxSetVersion: null,
mongo-express-1  |     commonWireVersion: 0,
mongo-express-1  |     logicalSessionTimeoutMinutes: null
mongo-express-1  |   },
mongo-express-1  |   code: undefined,
mongo-express-1  |   [Symbol(errorLabels)]: Set(0) {}
mongo-express-1  | }

If you name the service mongodb, then you should connect to it using mongodb, not mongo.