MongoNetworkError on first connect using Docker and NodeJS - node.js

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!

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.

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.

Related

Unable to use MongoDB on GCP cloud run and connect it to a NodeJS

I am testing a nodejs app on GCP functions and a MongoDB instance in GCP cloud run(I know this is not a good idea) but again this is a test. I am able to get the mongo image running on cloud run and I am given a service endpoint as https://mongodb.foo.bar.run.app and I have the container port in this cloud run service as 27017.
When I try and edit the mongoose.connect() method to include this service uri I am not able to connect with the app. I have the mongodb cloud run instance open to receive traffic from all traffic(Again not a good idea), but I still can't get the node app to connect.
I have setup the connection in node as mongoose.connect('mongodb://mongodb.foo.bar.run.app:27017/test');
but I get this error
Error: connect ETIMEDOUT <IP ADDRESS GOES HERE>:27017
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1146:16)
at TCPConnectWrap.callbackTrampoline (internal/async_hooks.js:129:14)
Emitted 'error' event on NativeConnection instance at:
at NativeConnection.Connection.error (/Users/foo/nodejs/bar/node_modules/mongoose/lib/connection.js:443:8)
at /Users/foo/nodejs/bar/node_modules/mongoose/lib/connection.js:472:15
at /Users/foo/nodejs/bar/node_modules/mongoose/lib/drivers/node-mongodb-native/connection.js:59:21
at /Users/foo/nodejs/bar/node_modules/mongodb/lib/db.js:232:14
at Server.<anonymous> (/Users/foo/nodejs/bar/node_modules/mongodb/lib/server.js:240:9)
at Object.onceWrapper (events.js:421:26)
at Server.emit (events.js:314:20)
at Pool.<anonymous> (/Users/foo/nodejs/bar/node_modules/mongodb-core/lib/topologies/server.js:308:68)
at Pool.emit (events.js:314:20)
at Connection.<anonymous> (/Users/foo/nodejs/bar/node_modules/mongodb-core/lib/connection/pool.js:115:12)
at Object.onceWrapper (events.js:421:26)
at Connection.emit (events.js:314:20)
at Socket.<anonymous> (/Users/foo/nodejs/bar/node_modules/mongodb-core/lib/connection/connection.js:144:49)
at Object.onceWrapper (events.js:421:26)
at Socket.emit (events.js:314:20)
at emitErrorNT (internal/streams/destroy.js:100:8) {
name: 'MongoError'
}
I tried to curl the service uri as
curl https://mongodb.foo.bar.run.app
and I get a 200 response in the mongo logs GET 200 466B but if I
curl mongodb.foo.bar.run.app
I get a 302 response code GET 302 0B.
I looked at the mongoose documentation but I couldn't figure what I was doing wrong. I know the documentation states user and password on the uri string, but when I run it locally on my docker engine I did not use a user and password and it worked.
As mentioned in the comments, it's not possible to run MongoDB on Cloud Run since as mentioned here:
Cloud Run can only receive HTTP requests or Pub/Sub push events.
Connecting to MongoDB requires a TCP protocol rather than HTTP, that's why every call to it would fail even if the port is reachable.
Cloud Run is in this sense more similar to AWS Lambda than it's to AWS Fargate as it's even billed on a per request basis rather than on resources consumed.

Connecting database (MongoDB) and backend (nodejs) running in docker containers [duplicate]

This question already has answers here:
MongoDB on with Docker "failed to connect to server [localhost:27017] on first connect "
(4 answers)
Closed 2 years ago.
First, I looked through several discussion with similar problems and it still did not work.
I have a mongodb docker container running, I did port forwarding with -p command
to be exact this is the command I ran:
sudo docker run -t -d -p 27017:27017 --name mongo mongo-0000
docker ps shows container running
9d9040a7bd66 mongo-0000 "docker-entrypoint.s…" 4 minutes ago Up 4 minutes 0.0.0.0:27017->27017/tcp mongo
as in another thread it was suggested to change mongodb bindip from 127.0.0.1 to 0.0.0.0 , which I also did (I tried both ways).
then I am trying to start up a backend app container with nodejs express backend app, I have had them working fine together on a VM, not on docker jet.
And i get following error
sudo docker run conduit-backend
Listening on port 3000
/ConduitReactApp/src/node_modules/mongodb/lib/server.js:261
process.nextTick(function() { throw err; })
^
Error [MongoError]: failed to connect to server [localhost:27017] on first connect
at Pool.<anonymous> (/ConduitReactApp/src/node_modules/mongodb-core/lib/topologies/server.js:313:35)
at Pool.emit (node:events:378:20)
at Connection.<anonymous> (/ConduitReactApp/src/node_modules/mongodb-core/lib/connection/pool.js:260:12)
at Object.onceWrapper (node:events:485:26)
at Connection.emit (node:events:378:20)
at Socket.<anonymous> (/ConduitReactApp/src/node_modules/mongodb-core/lib/connection/connection.js:162:49)
at Object.onceWrapper (node:events:485:26)
at Socket.emit (node:events:378:20)
at emitErrorNT (node:internal/streams/destroy:188:8)
at emitErrorCloseNT (node:internal/streams/destroy:153:3)
at processTicksAndRejections (node:internal/process/task_queues:81:21)
Emitted 'error' event on NativeConnection instance at:
at /ConduitReactApp/src/node_modules/mongoose/lib/connection.js:288:17
at NativeConnection.Connection.error (/ConduitReactApp/src/node_modules/mongoose/lib/connection.js:489:12)
at /ConduitReactApp/src/node_modules/mongoose/lib/connection.js:520:15
at /ConduitReactApp/src/node_modules/mongoose/lib/drivers/node-mongodb-native/connection.js:69:21
at /ConduitReactApp/src/node_modules/mongodb/lib/db.js:229:14
at Server.<anonymous> (/ConduitReactApp/src/node_modules/mongodb/lib/server.js:259:9)
at Object.onceWrapper (node:events:485:26)
at Server.emit (node:events:378:20)
at Pool.<anonymous> (/ConduitReactApp/src/node_modules/mongodb-core/lib/topologies/server.js:313:21)
at Pool.emit (node:events:378:20)
[... lines matching original stack trace ...]
at Socket.emit (node:events:378:20)
Also, inside app.js (in the backend app) for connecting to mongoDB it reads so
if(isProduction){
mongoose.connect(process.env.MONGODB_URI);
} else {
mongoose.connect('mongodb://localhost/conduit');
mongoose.set('debug', true);
}
What is still wrong here ?
Firstly, you should run your mongodb image after that you should mount mongo container to app container while run your app container. For example,
docker run -p xxxx:xxxx --link mongo:mongo <image-name>
And to connect mongodb like this, you should use a connection string like below,
'mongodb://mongo:27017/<db-name>'

Connecting to Mongodb docker container from another docker container

I am trying to connect mongo DB running in docker container from another docker container where my node js code is running.
So I run MongoDB docker using the following command:
docker run --name my-local-mongo -v mongo-data:/data/db -p 27017:27017 -d mongo
I can access from browser typing 0.0.0.0:27017, however when I try to connect from node js code I am getting following error. My url variable is:
var url = "mongodb://0.0.0.0:27017/surveydb";
{ MongoNetworkError: failed to connect to server [0.0.0.0:27017] on first connect [MongoNetworkError: connect **ECONNREFUSED 0.0.0.0:27017**]
at Pool.<anonymous> (/usr/src/appg08/node_modules/mongodb-core/lib/topologies/server.js:564:11)
at Pool.emit (events.js:182:13)
at Connection.<anonymous> (/usr/src/appg08/node_modules/mongodb-core/lib/connection/pool.js:317:12)
at Object.onceWrapper (events.js:273:13)
at Connection.emit (events.js:182:13)
at Socket.<anonymous> (/usr/src/appg08/node_modules/mongodb-core/lib/connection/connection.js:246:50)
at Object.onceWrapper (events.js:273:13)
at Socket.emit (events.js:182:13)
at emitErrorNT (internal/streams/destroy.js:82:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:50:3)
name: 'MongoNetworkError',
errorLabels: [ 'TransientTransactionError' ],
[Symbol(mongoErrorContextSymbol)]: {} }
Thanks.
var url = "mongodb://0.0.0.0:27017/surveydb";
is ip adddress is in your nodejs container only, so you should know what is ip address of mongo container or assign to ip address gateway of containers.
var url = "mongodb://172.17.0.1:27017/surveydb";
or
var url = "mongodb://ipaddressofmongocontainer:27017/surveydb";

MacOSX Express Mongoose mongoose.connect('mongodb://localhost:27071') -- ECONNREFUSED 127.0.0.1:27071 -- works fine on CLI

I'm pretty sure I've exhausted all methods here, followed the first 3 pages of Google and near every similar question here, and I am getting sick of reading every response to this "have you downloaded MongoDB?".
YES, it is downloaded, and running, and connectable from Robo3T and CLI mongo.
MongoDB shell version v4.0.2
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 4.0.2
YES, it was downloaded/installed using the recommended setup from source -- ie, home brew, brew services etc.
Jamies-MacBook-Pro:api jamiepoole$ brew services list
Name Status User Plist
mongodb started jamiepoole /Users/jamiepoole/Library/LaunchAgents/homebrew.mxcl.mongodb.plist
YES, I have tried connecting with localhost, 127.0.0.1, hell even the computer hostname / Jamies-MacBook-Pro.local
I have no doubt I'm doing something stupid or missed something simple, but I am pretty certain its not that.
const Database = Mongoose.connect('mongodb://localhost:27071/myproject');
---
I replaced project details from the below with ... -- so this isn't a setting / in the code.
{ MongoNetworkError: failed to connect to server [localhost:27071] on first connect [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27071]
at Pool.<anonymous> (/Users/.../node_modules/mongodb-core/lib/topologies/server.js:564:11)
at emitOne (events.js:116:13)
at Pool.emit (events.js:211:7)
at Connection.<anonymous> (/Users/.../node_modules/mongodb-core/lib/connection/pool.js:317:12)
at Object.onceWrapper (events.js:317:30)
at emitTwo (events.js:126:13)
at Connection.emit (events.js:214:7)
at Socket.<anonymous> (/Users/.../node_modules/mongodb-core/lib/connection/connection.js:246:50)
at Object.onceWrapper (events.js:315:30)
at emitOne (events.js:116:13)
at Socket.emit (events.js:211:7)
at emitErrorNT (internal/streams/destroy.js:64:8)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickCallback (internal/process/next_tick.js:180:9)
name: 'MongoNetworkError',
errorLabels: [ 'TransientTransactionError' ],
[Symbol(mongoErrorContextSymbol)]: {} }
---
At this point any help at all would be amazing. Seems like such a simple error but can't find any help anywhere.
Cheers
You have defined wrong MongoDB port in your code while your CLI is connecting to the right port which 27017. You can edit the code as follows
const Database = Mongoose.connect('mongodb://localhost:27017/myproject');

No living connections Error while Elasticsearch connections in nodejs

I am having this problem while connecting the elasticsearch connections.
var elasticsearch = require('elasticsearch');
var client = new elasticsearch.Client({ host: 'localhost:9200',
log: 'trace'});
Elasticsearch ERROR: 2016-07-19T19:09:26Z
Error: Request error, retrying -- connect ECONNREFUSED 127.0.0.1:9200
at Log.error (/root/git_build/FirstMoveChess/node_modules/elasticsearch/src/lib/log.js:225:56)
at checkRespForFailure (/root/git_build/FirstMoveChess/node_modules/elasticsearch/src/lib/transport.js:195:18)
at HttpConnector. (/root/git_build/FirstMoveChess/node_modules/elasticsearch/src/lib/connectors/http.js:154:7)
at ClientRequest.bound (/root/git_build/FirstMoveChess/node_modules/lodash-node/modern/internals/baseBind.js:56:17)
at emitOne (events.js:96:13)
at ClientRequest.emit (events.js:188:7)
at Socket.socketErrorListener (_http_client.js:308:9)
at emitOne (events.js:96:13)
at Socket.emit (events.js:188:7)
at emitErrorNT (net.js:1272:8)
at _combinedTickCallback (internal/process/next_tick.js:74:11)
at process._tickCallback (internal/process/next_tick.js:98:9)
Elasticsearch TRACE: 2016-07-19T19:09:27Z
-> HEAD http://localhost:9200/
I understand this question is quite old but I wanted to share how you can solve this problem.
If you are locally using elasticsearch
First thing you must do is running elasticsearch on your machine.
Error: Request error, retrying -- connect ECONNREFUSED 127.0.0.1:9200
Because above message indicates that you're not running elasticsearch locally.
So, visit the link and follow the insturction.
Docker environment
It gets much trickier here.
First, follow the instruction here.
And in case you're using node.js elasticsearch client, you have to specify elasticsearch host as 172.24.0.1.
If you use container_name or private IP of container in docker-compose.yml, it won't work.
In case of Docker Container Environment after changing from http://localhost:9200 to http://ipaddress:9200 in docker-compose.yml
please change the following live in docker-compose.yml that is related to CORS
Change this
** http.cors.allow-origin=/https?://localhost(:[0-9]+)?/ **
into this
*- http.cors.allow-origin= **

Resources