sailsJs mongodb connection error in docker container - mongodb

Unable to connect sailsJs dockerimage to mongodb.
I am having issue while connecting mongodb running on local machine, not as mongo image.
mongodb is running on localhost:27017
error: Error: Failed to connect to MongoDB. Are you sure your configured Mongo instance is running?
Error details: {
Error: connect ECONNREFUSED 127.0 .0 .1: 27017
at Object.exports._errnoException(util.js: 1020: 11)
at exports._exceptionWithHostPort(util.js: 1043: 20)
at TCPConnectWrap.afterConnect[as oncomplete](net.js: 1090: 14)
name: 'MongoError',
message: 'connect ECONNREFUSED 127.0.0.1:27017'
}

If the container is running in a docker engine in the same host where your MongoDB server is running, you should try to run your docker container using --net=host if you want your container to be able to reach 127.0.0.1/localhost in the same host. I nice answer can be found here.
If you're using docker engine in a VM you may have to change the localhost IP for the public/private IP assigned to your mongodb server host.

Related

Unable to access mongodb running in a docker from local machine

I am trying to access the mongodb running in a docker from my local machine however I am seeing this error message:
Error: couldnt connect to server localhost:27017, connection attempt failed: SocketException: Error connecting to localhost:27017 (127.0.0.1:27017) :: caused by :: No connection could be made because the target machine actively refused it.
commands tried to used to access mongo on my local machine:
mongo localhost:27017
mongo 172.17.0.2:27017(container's ip)
command used to run mongodb on docker
docker run -d -p 27017:27017 mongo:latest
Please advice. Thank you.

Error when connecting to Mongo DB Hosted in a container

Ive installed Docker locally (mac) and tried to run a docker container using the following command.
docker run -it -p 27017:27017 mongoImageId
But when I tried to login in to the container via the mongo terminal client, Im getting the following error.
mongo --port 27017 -u "userName" -p "password" --authenticationDatabase "sampleDBName"
MongoDB shell version v3.6.5
connecting to: mongodb://127.0.0.1:27017/
2018-07-23T13:41:53.423+0530 E QUERY [thread1] Error: network error while attempting to run command 'isMaster' on host '127.0.0.1:27017' :
connect#src/mongo/shell/mongo.js:251:13
#(connect):1:6
exception: connect failed
But if I try to build the same image in an aws ec2 instance, it connects without a hazel. Does any one else also encountered the same issue and was able to find the root cause ?
Ref.
[1] - https://github.com/aashreys/docker-mongo-auth

How to connect to mongodb as a docker image from another container app

Firstly I pulled mongoDb image
sudo docker pull mongo
and run docker mongodb container:
sudo docker run --name database -d -p 27017:27017 mongo --noauth --bind_ip=0.0.0.0
I see that my docker container works:
300b779a63a9 mongo "docker-entrypoint..." 30 minutes ago Up 30 minutes
and now Im trying to connect to it with my docker app
sudo docker run -v ~/Desktop/Template/universal_skillskill -it --link 300b779a63a9:300b779a63a9 alexa
and... Im getting a message that the connection is refused :(
Uncaught exception: MongoError: failed to connect to server [0.0.0:27017] on first connect [MongoError: connect ECONNREFUSED 0.0.0.0:27017]
MongoError: failed to connect to server [0.0.0:27017] on first connect [MongoError: connect ECONNREFUSED 0.0.0.0:27017]
I checked and my app works with mongodb installed locally but it doesnt communicate with docker mongoDb container :( What am I doing wrong? Could somebody smart help me please :)
You need to connect to 300b779a63a9:27017 as this is the linked name that you gave for the mongodb.
try to connect with the name of your container database and not with IP.
Adding to previous answers if you are starting your container with these additional parameter for authentication
- MONGO_INITDB_ROOT_USERNAME=user
- MONGO_INITDB_ROOT_PASSWORD=password
then use authSource=admin parameter as well
only passing credentials wouldn't work.
mongodb://<user>:<password>#<container_name>:27017/$<DB>?authSource=admin
I faced this issue. thought, this might help someone.

mongo - couldn't connect to 127.0.0.1 - ip changed

I installed mongoDB and changed the IP to 10.3.Y.XX.
When i want to start mongo from terminal (> mongo), this error occurs:
Error: couldn't connect to server 127.0.0.1:27017 (127.0.0.1),
connection attempt failed at src/mongo/shell/mongo.js:146
exception: connect failed
Any idea how to solve this?
If you want to connect to something other than the default host (which is "localhost"), you need to tell your Mongo client application about it.
For example, on the command line you can do
mongo --host 10.3.x.y db_name

Can't connect to mongodb instance running on docker-machine

I'm fairly new to docker and I'm trying to connect from my machine to a mongo server running on a docker-machine.
I started the docker image as it is documented in the official mongo repository:
docker run --name my-mongo -d mongo
Because I'm running on a docker-machine I'm using the machine ip (got it with docker-machine ip dev) and default mongo port and type:
mongo --host 192.168.99.100 --port 27017
And I'm getting this response:
2015-09-28T17:20:14.438+0300 warning: Failed to connect to 192.168.99.100:27017, reason: errno:61 Connection refused
2015-09-28T17:20:14.439+0300 Error: couldn't connect to server 192.168.99.100:27017 (192.168.99.100), connection attempt failed at src/mongo/shell/mongo.js:148
exception: connect failed
From the logs I can see that mongo started off just fine:
2015-09-28T14:12:47.550+0000 I STORAGE [FileAllocator] creating directory /data/db/_tmp
2015-09-28T14:12:47.551+0000 I STORAGE [FileAllocator] done allocating datafile /data/db/local.0, size: 64MB, took 0 secs
2015-09-28T14:12:47.554+0000 I NETWORK [initandlisten] waiting for connections on port 27017
What am I missing - is it my configuration or the way I'm trying to start the mongo shell (I also tried to connect with node application - no success either).
Check the IP could be ping from your system. if so
Have done port forwarding in Dockerfile
You have to map the docker port which runs the mongo DB to a system port and need to connect to that port if you are not connecting from the local system