Robert
Robert

Reputation: 36773

MongoError: connect ECONNREFUSED in Docker

I have my docker-compose orchestration and I'm getting this error:

connection error: { MongoError: failed to connect to server [172.17.0.2:27018] on first connect [MongoError: connect ECONNREFUSED 172.17.0.2:27018]

This is the code in server.js:

mongoose.connect('mongodb://mongodb:27018');

This happen only when I customize the command of my docker container:

docker-compose.yml:

version: "3"
services:
  app:
    build: ./my-node-app
    depends_on:
      - mongodb
  mongodb:
    image: mongo:3.5
    command: mongod --port 27018

If I remove the --port 27018 and point to the default 27017 the error gets fixed.

What could be happening?

Upvotes: 2

Views: 1742

Answers (1)

Robert
Robert

Reputation: 36773

You're missing: --bind_ip_all

It seems that newer versions (>3.5) of the mongod daemon listen only to localhost by default.

Your are overriding this:

CMD ["mongod", "--bind_ip_all"]

So, also put "--bind_ip_all" in your docker-compose.yml:

mongodb:
  image: mongo:3.5
  command: mongod --port 27018 --bind_ip_all

Upvotes: 3

Related Questions