MongoServerSelectionError: getaddrinfo ENOTFOUND

Viewed 1225

I have some issue on connect to mongodb with docker and NodeJS driver.

Here is my docker-compose.yml file:

version: "3.3"
services:
  mongodb:
    image: mongo:4.0.20
    ports:
        - 27017:27017
    command: mongod --replSet rs0
    deploy:
      replicas: 1
      restart_policy:
        condition: "on-failure"

And I launch my service with docker stack deploy -c ./docker-compose.yml --with-registry-auth XXX

Then, when I'm sure mongo is well started, I run:

docker exec $(docker ps -q -f name=mongodb) mongo local --eval "rs.initiate()" which initialize the replicat set.

Here is my nodejs test file:

const MongoClient = require('mongodb');

(async () => {
  let db;
  try {
    db = (await MongoClient(
      'mongodb://localhost:27017/qos',
      {
        useNewUrlParser: true,
        useUnifiedTopology: true
      }
    )).db();
    console.log('connected');
  } catch (error) {
    console.trace(error)
  }
})()

But when I run the test.js file, I see:

Trace: MongoServerSelectionError: getaddrinfo ENOTFOUND 272b05abb632
    at Timeout._onTimeout (node_modules/mongodb/lib/core/sdam/topology.js:438:30)
    at listOnTimeout (internal/timers.js:554:17)
    at processTimers (internal/timers.js:497:7) {
  reason: TopologyDescription {
    type: 'ReplicaSetNoPrimary',
    setName: 'rs0',
    maxSetVersion: 1,
    maxElectionId: 7fffffff0000000000000001,
    servers: Map { 'bcc9e46ea248:27017' => [ServerDescription] },
    stale: false,
    compatible: true,
    compatibilityError: null,
    logicalSessionTimeoutMinutes: null,
    heartbeatFrequencyMS: 10000,
    localThresholdMS: 15,
    commonWireVersion: 7
  }
}
    at test.js:15:13

I check that 272b05abb632 is the good container id, and on my docker process I see:

2020-11-10T16:34:49.019360315Z XXX_mongodb.1.awsb46tji1lg@docker-desktop    | 2020-11-10T16:34:49.018+0000 I NETWORK  [listener] connection accepted from 10.0.0.2:49082 #4 (2 connections now open)
2020-11-10T16:34:49.027564962Z XXX_mongodb.1.awsb46tji1lg@docker-desktop    | 2020-11-10T16:34:49.027+0000 I NETWORK  [conn4] received client metadata from 10.0.0.2:49082 conn4: { driver: { name: "nodejs", version: "3.6.3" }, os: { type: "Darwin", name: "darwin", architecture: "x64", version: "19.6.0" }, platform: "'Node.js v12.19.0, LE (unified)" }
2020-11-10T16:34:49.033185739Z XXX_mongodb.1.awsb46tji1lg@docker-desktop    | 2020-11-10T16:34:49.032+0000 I NETWORK  [conn4] end connection 10.0.0.2:49082 (1 connection now open)

I don't understand why with useUnifiedTopology, it's not work :(

I request your help !

I also tried to add mongod.conf in my container via docker-compose, but it doesn't work too:

version: "3.3"
services:
  mongodb:
    image: mongo:4.0.20
    ports:
        - 27017:27017
    command: mongod --config /etc/mongod.conf 
    volumes:
      - "./mongod.conf:/etc/mongod.conf"
    deploy:
      replicas: 1
      restart_policy:
        condition: "on-failure"
security:
  authorization: 'enabled'

net:
  port: 27017
  bindIp: 0.0.0.0   #default value is 127.0.0.1

replication:
  replSetName: rs0
0 Answers
Related