connect to mongodb in docker from node app in AWS SAM - node.js

I am getting errors connecting to mongodb running in a docker container from my Nodejs app running in AWS SAM (used to say "in my host").
I run mongodb like:
$ docker run --name mongo-myapp --rm -d -p 27018:27017 mongo
and I see the results:
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ab8248d17d2d mongo "docker-entrypoint.s…" 6 minutes ago Up 6 minutes 0.0.0.0:27018->27017/tcp mongo-myapp
~~I can successfully connect and insert data using clients running on my host like MongoDB Compass and Robo 3T Community Edition, specifying port 27018.~~
When I attempt to connect with this code running on my host, (not in a docker container):
const { MongoClient } = require('mongodb');
const mongoConnection = 'mongodb://127.0.0.1:27018';
MongoClient.connect(mongoConnection, (err, db) => {
if (err) {
console.error("Failed to connect to mongodb server", err);
return reject(err);
}
console.log("Connected successfully to mongodb server");
resolve(db);
});
I always see the error:
MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27018
I get the same error using another port with all steps, like 27017.
UPDATE
It turns out my code was not running on the host. It was running in another docker container. I did not realize AWS SAM would put my code into a docker container so I did not mention it in the original question.
Now I run my code with mocha test to make sure it will run on my host, and it connects to the mongo database with no problems.
When I launched a local server using AWS SAM's start-api, I had problems. Perhaps the solution will be to specify a network when starting the mongo container as well as the SAM environment.

Now that the problem is known that the Nodejs code was running within a docker container created by AWS SAM, we can help the Nodejs code connect with Mongodb running in a separate docker container with a port exposed on the host with at least one solution:
Change the connection string to mongodb://host.docker.internal:27018 which helps the code in the container to use a service running on the host.

Install the necessary dependency for mongodb in node.js https://www.npmjs.com/package/mongodb
const MongoClient = require('mongodb').MongoClient;
const url = "mongodb://localhost:27018/testdb";
MongoClient.connect(url, function(err, db) {
if (err) throw err;
console.log("Database created!");
db.close();
});

Since you are able to connect from other clients in the same host, I assume that the container is bound to specific external facing ip of the host.
Can you try connecting to the ip of the host instead of 127.0.0.1
Eg: mongodb://external-ip:27018
Though the mapping -p 27018:27017 should bind the port to all ips, you can enforce this by -p 0.0.0.0:27018:27017.

Related

Connect to MongoDb that is running in a Docker Container that is running inside a server

I am trying to connect to MongoDb that is running inside a docker container. This docker Container is running inside a server, let us say (192.158.8.02). How to i write a connection string to connect to the MongoDb.
from pymongo import MongoClient
client = MongoClient(“mongodb://mongo:27017/”)
here mongo is container name
how to solve this?

My docker container failing to connect to mongodb database container [duplicate]

I'm new to docker. I'm trying to create a MongoDB container and a NodeJS container. My file looks:
version: '2'
services:
backend:
image: node:5.11-onbuild
ports:
- "3001:3001"
volumes:
- .:/code
working_dir: "/code"
links:
- mongodb
mongodb:
image: mongo:3.3
expose:
- 27017
It should run npm install and then node ..
But docker-compose up ends up with [MongoError: connect ECONNREFUSED 127.0.0.1:27017] while the command node ..
I think this is because of the bind_ip = 127.0.0.1 in the file /etc/mongod.conf. Is this right?
I use boot2docker on a Win10 system.
How can I solve this problem so that node can connect to the MongoDB?
In your backend app, connect to mongodb:27017 instead of 127.0.0.1:27017. Where 'mongodb' is the name of your service within docker-compose.yml.
I recently encountered similar issue. I am running docker toolbox under win 10 and this is how it worked for me:
1) I had to verify which URL my default docker machine is using. This can be checked by running docker-machine ls command. It will list available machines:
NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS
default * virtualbox Running tcp://192.168.99.100:1234 v17.06.0-ce
rancher-client - virtualbox Stopped Unknown
rancher-server - virtualbox Stopped Unknown
2) When running mongodb image specify the port mapping
docker run -d -it -p 27017:27017 mongo
3) At that point the valid mongo url would look something like this
var dbhost = 'mongodb://192.168.99.100:27017/test
where 192.168.99.100 was the default machine URL from the point 1)
Hope it helps someone.
Most likely, yes. 127.0.0.1 points to localhost inside the mongodb container, so is not accessible from outside the container. Binding to 0.0.0.0 will probably work.
With the link you specified in the docker-compose.yml, your backend container should then be able to connect to the mongo container through mongodb:27017
You have to tell the container to use it's own IP Address instead of localhost.
For example, let's assume you generated scaffold code with expressjs, you have to write in routes/index.js
var mongodb = require('mongodb');
router.get('/thelist', function(req, res){
// Get a Mongo client to work with the Mongo server
var MongoClient = mongodb.MongoClient;
// Define where the MongoDB server is
var url = 'mongodb://172.17.0.5:27017/dbname';
// Connect to the server
MongoClient.connect(url, function (err, db) {
.........
where 172.17.0.5 is the $CONTAINER_IP
you can find the container ip via
$ docker inspect $CONTAINER_HOSTNAME | grep IPAddress
If you still can't understand you can take a peek at my Docker NodeJS and MongoDB app

Keep hitting connection error with docker for node mongo app [duplicate]

I'm new to docker. I'm trying to create a MongoDB container and a NodeJS container. My file looks:
version: '2'
services:
backend:
image: node:5.11-onbuild
ports:
- "3001:3001"
volumes:
- .:/code
working_dir: "/code"
links:
- mongodb
mongodb:
image: mongo:3.3
expose:
- 27017
It should run npm install and then node ..
But docker-compose up ends up with [MongoError: connect ECONNREFUSED 127.0.0.1:27017] while the command node ..
I think this is because of the bind_ip = 127.0.0.1 in the file /etc/mongod.conf. Is this right?
I use boot2docker on a Win10 system.
How can I solve this problem so that node can connect to the MongoDB?
In your backend app, connect to mongodb:27017 instead of 127.0.0.1:27017. Where 'mongodb' is the name of your service within docker-compose.yml.
I recently encountered similar issue. I am running docker toolbox under win 10 and this is how it worked for me:
1) I had to verify which URL my default docker machine is using. This can be checked by running docker-machine ls command. It will list available machines:
NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS
default * virtualbox Running tcp://192.168.99.100:1234 v17.06.0-ce
rancher-client - virtualbox Stopped Unknown
rancher-server - virtualbox Stopped Unknown
2) When running mongodb image specify the port mapping
docker run -d -it -p 27017:27017 mongo
3) At that point the valid mongo url would look something like this
var dbhost = 'mongodb://192.168.99.100:27017/test
where 192.168.99.100 was the default machine URL from the point 1)
Hope it helps someone.
Most likely, yes. 127.0.0.1 points to localhost inside the mongodb container, so is not accessible from outside the container. Binding to 0.0.0.0 will probably work.
With the link you specified in the docker-compose.yml, your backend container should then be able to connect to the mongo container through mongodb:27017
You have to tell the container to use it's own IP Address instead of localhost.
For example, let's assume you generated scaffold code with expressjs, you have to write in routes/index.js
var mongodb = require('mongodb');
router.get('/thelist', function(req, res){
// Get a Mongo client to work with the Mongo server
var MongoClient = mongodb.MongoClient;
// Define where the MongoDB server is
var url = 'mongodb://172.17.0.5:27017/dbname';
// Connect to the server
MongoClient.connect(url, function (err, db) {
.........
where 172.17.0.5 is the $CONTAINER_IP
you can find the container ip via
$ docker inspect $CONTAINER_HOSTNAME | grep IPAddress
If you still can't understand you can take a peek at my Docker NodeJS and MongoDB app

MongoNetworkError: failed to connect to server [localhost:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017]

I'm new in nodeJS, started learning by following a trailer on youtube, everything goes well until I added the connect function if mongodb,
mongo.connect("mongodb://localhost:27017/mydb")
when I run my code on cmd (node start-app), get the following error,
MongoNetworkError: failed to connect to server [localhost:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017]
Could someone explain me which step I missed ?
my code :
var express = require("express");
var MongoClient = require('mongodb');
var url = "mongodb://localhost:27017/mydb";
var webService = require("./webService");
var server = express();
MongoClient.connect(url, function (err, db) {
if (err) throw err;
console.log("Database created!");
db.close();
});
server.use(express.urlencoded({ extended: true }));
server.set('views', __dirname);
server.get('/', function (request, response) {
response.sendFile(__dirname + '/MainPage.html');
});
server.get('/Sign', function (request, response) {
response.render(__dirname + '/Sign.ejs');
});
server.post("/signUp", webService.signUp);
server.post("/createUser", webService.createUser);
server.listen(5500);
You have to install MongoDB database server first in your system and start it.
Use the below link to install MongoDB
https://docs.mongodb.com/manual/installation/
If you have installed MongoDB check if the server is in which state (start/stop). Try to connect through mongo shell client.
Many of them don't add this, especially in AWS EC2 Instance, I had the same issue and tried different solutions.
Solution: one of my database URL inside the code was missing this parameter 'authSource', adding this worked for me.
mongodb://myUserName:MyPassword#ElasticIP:27017/databaseName?authSource=admin
I faced same issue but after a lot of RND. I found that whts the problem so run this command on your terminal.
sudo service mongod start
then run mongo on terminal
After trying EVERY solution google came up with on stack overflow, I found what my particular problem was. I had edited my hosts file a long time ago to allow me to access my localhost from my virtualbox.
Removing this entry solved it for me, along with the correct installation of mongoDB from the link given in the above solution, and including the correct promise handling code:
mongoose.connect('mongodb://localhost/testdb').then(() => {
console.log("Connected to Database");
}).catch((err) => {
console.log("Not Connected to Database ERROR! ", err);
});
Following the logic behind #CoryM's answer above :
After trying EVERY solution google came up with on stack overflow, I found what my particular problem was. I had edited my hosts file a long time ago to allow me to access my localhost from my virtualbox.
Removing this entry solved it for me...
I had edited my hosts file too for Python Machine Learning setup 2 months ago. So instead of removing it because I still need it, I use 127.0.0.1 in place of localhost and it worked :
mongoose.connect('mongodb://127.0.0.1/testdb')
Your IP address probably changed.
If you've recently restarted your modem, this changes your IP which was probably whitelisted on Atlas.
Soooo, you'll need to jump back onto Atlas and add your new IP address to the whitelist under Security>Network Access.
This had occurred to me and I have found out that it was because of faulty internet connection. If I use the public wifi at my place, which blocks various websites for security reasons, Mongo refuses to connect. But if I were to use my own mobile data, I can connect to the database.
If the mongoDB server is already installed and if you are unable to connect from a remote host then follow the below steps,
Login to your machine, open mongodb configuration file located at /etc/mongod.conf and change the bindIp field to specific ip / 0.0.0.0 , after that restart mongodb server.
sudo vi /etc/mongod.conf
The file should contain the following kind of content:
systemLog:
destination: file
path: "/var/log/mongodb/mongod.log"
logAppend: true
storage:
journal:
enabled: true
processManagement:
fork: true
net:
bindIp: 127.0.0.1 // change here to 0.0.0.0
port: 27017
setParameter:
enableLocalhostAuthBypass: false
Once you change the bindIp, then you have to restart the mongodb, using the following command
sudo service mongod restart
Now you'll be able to connect to the mongodb server, from remote server.
I solved this problem by upgrading major version of mongoose:
Before doing this, make sure (using mongo shell) that you have the correct URL and a running mongo server is available at that URL and the problem still persists.
"dependencies": {
- "mongoose": "^5.4.13",
+ "mongoose": "^6.2.4",
}
just run mongod in terminal on the base folder if everything has been set up like installing mongo db and the client for it like mongoose. After running the command run the project file that you are working on and then the error shouldn't appear.
You can check detail of error by running this command
sudo service mongod status
if error is something like this
Failed to unlink socket file /tmp/mongodb-27017.sock Unknown error
Fatal Assertion 40486 at src/mongo/transport/transport_layer_asio.cpp 670
simply running this will resolve your issue
rm /tmp/mongodb-27017.sock
I don't know if this might be helpful, but when I did this it worked:
Command mongo in terminal.
Then I copied the URL which mongo command returns, something like
mongodb://127.0.0.1:*port*
I replaced the URL with this in my JS code.
first create folder by command line mkdir C:\data\db (This is for database)
then run command mongod --port 27018 by one command prompt(administration mode)- you can give name port number as your wish
I had this issue while working at the local Starbucks and I remembered that when I initially set up my database through Mongo Atlas. I set my IP address to be able to access the database. After looking through several threads, I changed my IP address on Atlas and the issue went away. Hope this helps someone.
This worked for me.
mongoose.Promise = global.Promise;
.connect(
"mongodb://127.0.0.1:27017/mydb",
{ useNewUrlParser: true, useCreateIndex: true, useUnifiedTopology: true}).then(db => {
console.log("Database connected");
}).catch(error => console.log("Could not connect to mongo db " + error));
I was using localhost, so i changed it to:
mongodb://127.0.0.1:27017/mydb
My problem was the wrong port number for mongoDB server.
I had:
DATABASE_URL= "mongodb://localhost:3000/node-express-mongodb-server"
in my .env file (my environmental variables), but I had written it before running mongoDB server. So when I ran the mongoDB server, it wrote a different port number and I had to change it. I changed it to the right port number (which was written on my cmd window by mongoDB):
DATABASE_URL= "mongodb://localhost:27017/node-express-mongodb-server"
and now it works fine.
if you are a Mac user just upgrade your homeBrew from terminal:
$ brew upgrade
$ mongod --config usr/local/etc/mongod.config
$ Xcode-select --install
$ mongo
1) If you haven't installed mongodb, install it.
2) open a new terminal, type "mongo". This is going to connect you to a MongoDB instance running on your localhost with default port 27017:
mongoose.connect('mongodb://localhost:27017/').then(() => {
console.log("Connected to Database");
}).catch((err) => {
console.log("Not Connected to Database ERROR! ", err);
});
Better just connect to the localhost Mongoose Database only and create your own collections. Don't forget to mention the port number. (Default: 27017)
For the best view, download Mongoose-compass for MongoDB UI.
This one helped me.
Try creating a new folder, if your MongoDB is installed in C:\Program Files the folder should be called db and in a folder data.
C:\data\db
When you start the mongod there should be a log where the db 'isnt found'.
So when none of the above solutions worked for me, after installing everything correctly, I thought to restart the system.
It's working now.
Note that I did everything said above, but no luck. The only restart worked for me.!!
You may also want to restart once.
You have to install MongoDB database server first in your system and start it.
Use the below link to install MongoDB
If you have already installed MongoDB database in your system then you have to check that your DB is in start position or not with the help of following steps:
press CTRL + Shift + Esc
go to the service tab and search for Mongo
check the status - it may be stopped. So click on the Services tab at the bottom right corner and again search for MongoDB
Click on it and start the DB by right click or in left panel.
If the error happens on macbook run this command to keep the mongodb server running.
mongod --config /usr/local/etc/mongod.conf --fork
The issue majorly is that your mongodb server is rejecting the connection it might be that the server is not on/active eventhough it has been installed on your macbook.
In my case the problem was that there was another instance of mongoDB server running I had shutdown my computer without stopping the server hence when I tried running mongosh it gives me that error. Try restarting the computer it will shutdown all the servers and the erro was gone.
I was trying to connect, without starting the service.
This is how i fixed the error (MacOS env).
$ brew services start mongodb-community#6.0
$ mongosh // connected to db and fixed the error.
$ brew services stop mongodb-community#6.0
For me the problem resolved when I started the MongoDB on port other than 27017. Even though nothing was running on 27017 but the problem resolved when I started it on another port.
To do that navigate to the /etc/mongod.conf and change the port: 27017 to some other port like port: 27019.
Then restart the service by:
sudo systemctl restart mongod.service.
And then try to connect to MongoDB by specifying the --port parameter like:
mongod --port 27019, or
mongo --port 27019
Best!
this was my erros:
Connecting to: mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.6.2
MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017
SOLUTION:
The best Answes is if you using mac M1 and M2
use restrat with sudo like below
MongoNetworkError solution
sudo brew services restart mongodb-community#6.0
I connected to a VPN and the connection accomplished. I was using school's WiFi which has some restrictions apparently.
I guess you must be connecting to cloud.mongodb.com to your cluster.
One quick fix is to go to the connection tab and add your current IP address(in the cluster portal of browser or desktop app). The IP address must have changed due to a variety of reasons, such as changing the wifi.
Just try this approach, it worked for me when I got this error.
You need to initialize your mongoDB database first, you can run "mongod" in your terminal and then it will be working fine.

Able to connect to MongoDB running in a Docker container locally, but not from another container

Basically what the title says: I am able to connect to my MongoDB database running in a Docker container locally, but I am unable to do so from within another Docker container. I am getting the error "MongoError: failed to connect to server {server address}:27017 on first connect"
Here is my config file:
const mongoose = require('mongoose');
mongoose.Promise = global.Promise
let mongoURI = {
development: 'mongodb://{server address}:27017/database,
}
const db = mongoose.connect(mongoURI.development, function(err, res) {
if(err) {
console.log('Error connecting to the database. ' + err);
} else {
console.log('Connected to Database: ' + mongoURI.development);
}
});
module.exports = db;
It's not clear from your question what configuration of Docker containers you're working with. There are a couple of possibilities:
Two Docker containers, one running a MongoDB image and another running your application, linked together via the --link option.
Assuming you gave the Mongo container a name via the --name option, you should be able to access the MongoDB via the container name: mongodb://{container name}:27017/database.
Two Docker containers NOT linked with the --link option.
In this case, you will need to have exposed the MongoDB port on the Mongo container with the -p option AND provided the application container with an IP address of your Docker host: mongodb://{docker host IP}:27017/database. Note that localhost will not work, since inside your application container localhost will refer to the application container. You could configure an entry in the application's /etc/hosts to give your Docker host a name.
MongoDB running in a container; application running on the Docker host (or some other server).
This is just a variation of #2 above. Again, you need to expose the ports and ensure that the application know how to find the host where the MongoDB container is running. In this case you could use localhost if the application in running on the Docker host.
Solved it, I should have specified this earlier: My configuration was the two containers not linked with the --link option, and MongoDB port exposed. The issue was that my company uses a proxy, and I was not disabling it properly when connecting to a server running on our domain.

Resources