Connecting to Mongodb docker container from another docker container - node.js

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";

Related

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>'

MongoNetworkError on node js and mongo db atlas

I have made a MEAN stack project. And this project was successfully run some days ago. But suddenly it gives MongoNetworkError. I do not change anything. I use mongo atlas. The error description is given below.
error MongoNetworkError: failed to connect to server [cluster0-shard-00-01-s9niu.mongodb.net:27017] on first connect [MongoNetworkError: connection 5 to cluster0-shard-00-01-s9niu.mongodb.net:27017 closed
at TLSSocket. (D:\Angular\meal-system-server\node_modules\mongodb\lib\core\connection\connection.js:372:9)
at Object.onceWrapper (events.js:300:26)
at TLSSocket.emit (events.js:210:5)
at net.js:658:12
at TCP.done (_tls_wrap.js:481:7) {
name: 'MongoNetworkError',
}]
at Pool. (D:\Angular\meal-system-server\node_modules\mongodb\lib\core\topologies\server.js:438:11)
at Pool.emit (events.js:210:5)
at D:\Angular\meal-system-server\node_modules\mongodb\lib\core\connection\pool.js:561:14
at D:\Angular\meal-system-server\node_modules\mongodb\lib\core\connection\pool.js:1008:9
at callback (D:\Angular\meal-system-server\node_modules\mongodb\lib\core\connection\connect.js:97:5)
at D:\Angular\meal-system-server\node_modules\mongodb\lib\core\connection\connect.js:124:7
at _callback (D:\Angular\meal-system-server\node_modules\mongodb\lib\core\connection\connect.js:349:5)
at Connection.errorHandler (D:\Angular\meal-system-server\node_modules\mongodb\lib\core\connection\connect.js:365:5)
at Object.onceWrapper (events.js:300:26)
at Connection.emit (events.js:210:5)
at TLSSocket. (D:\Angular\meal-system-server\node_modules\mongodb\lib\core\connection\connection.js:370:12)
at Object.onceWrapper (events.js:300:26)
at TLSSocket.emit (events.js:210:5)
at net.js:658:12
at TCP.done (_tls_wrap.js:481:7) {
name: 'MongoNetworkError',
}
Seems like an error with the certificate, mongodb atlas recently made changes in the certificates.
Which language you are using for this application and what is the version of the language ?
Please refer this document for more information : Mongodb Atlas Security

MongoNetworkError on first connect using Docker and NodeJS

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.

MongoNetworkError : failed to connect to server

First time working with mongoDb inside a Node.js app (using mLab) but i'm unable to connect.
Same exact code works just fine if i try to connect to mongoDB Atlas.
// not working with mLab link and credentials
mongoose.connect(mLabConnectionString)
.then(success => console.log(success))
.catch(error => console.log(error))
// working with atlas link and credentials
mongoose.connect(atlasConnectionString)
.then(success => console.log(success))
.catch(error => console.log(error))
Followed these steps https://docs.mlab.com/troubleshooting-connection-issues/
and i do have network access, i'm using database user credentials to connect (not my mLab account credentials) and my password just contains chars and numbers. That's the error i'm getting :
{ MongoNetworkError: failed to connect to server [***my_database***]
on first connect [MongoNetworkError: getaddrinfo ENOTFOUND ***my_database*** ***my_database***]
at Pool.<anonymous> (C:\Users\akain\Desktop\devconnector\node_modules\mongodb-core\lib\topologies\server.js:564:11)
at Pool.emit (events.js:182:13)
at Connection.<anonymous> (C:\Users\akain\Desktop\mongotest\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> (C:\Users\akain\Desktop\mongotest\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)
at process._tickCallback (internal/process/next_tick.js:63:19)
name: 'MongoNetworkError',
errorLabels: [ 'TransientTransactionError' ],
[Symbol(mongoErrorContextSymbol)]: {} }
Now the weird part : If i try to connect to mLab using mongo shell, not only everything works (from the shell), but i'm also able to connect without problem from my app. As soon as i close the shell and restart the app i get the error again.
I've also tried using mongoDb Node client instead of mongoose getting the same exact behaviour.

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