0

I'm still incredibly new to coding. I'm trying to launch an app using docker, everything goes well until I get this error:

{ MongoNetworkError: failed to connect to server [localhost:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017]
    at Pool.<anonymous> (/app/node_modules/mongodb/lib/core/topologies/server.js:433:11)
    at Pool.emit (events.js:198:13)
    at createConnection (/app/node_modules/mongodb/lib/core/connection/pool.js:577:14)
    at connect (/app/node_modules/mongodb/lib/core/connection/pool.js:1007:11)
    at makeConnection (/app/node_modules/mongodb/lib/core/connection/connect.js:31:7)
    at callback (/app/node_modules/mongodb/lib/core/connection/connect.js:247:5)
    at Socket.err (/app/node_modules/mongodb/lib/core/connection/connect.js:276:7)
    at Object.onceWrapper (events.js:286:20)
    at Socket.emit (events.js:198:13)
    at emitErrorNT (internal/streams/destroy.js:91:8)
    at emitErrorAndCloseNT (internal/streams/destroy.js:59:3)
    at process._tickCallback (internal/process/next_tick.js:63:19)
  name: 'MongoNetworkError',
  [Symbol(mongoErrorContextSymbol)]: {} }
[nodemon] clean exit - waiting for changes before restart

I have mongodb running as a server on Windows 10, and I also have it running in another window, so I'm not sure what could be causing it. I've tried all solutions on this page, but to no avail. Thanks in advance!

USVDH
  • 1

2 Answers2

0

Localhost is the container, not your machine. When working with containers, you need to think of container as a separate machine and use a full network adressess to communicate between your machine and container.

Miq
  • 3,931
  • 2
  • 18
  • 32
0

I was using a similar concept like yours. Try to run both node-app and Mongo service under the same network.

In Mongo connection string, the Hostname should be the same as your service name mentioned in docker-compose.yml file.

enter image description here

enter image description here

Hardik Raval
  • 3,406
  • 1
  • 26
  • 28