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

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

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.

How do I access a remote aws lightsail mongodb over ssh tunnel

I have a Lightsail AWS instance up and running with a MEAN stack. I have an existing MEAN stack running on a different network. At the moment the node server.js connects to localhost for the mongo bit (on same machine) and all I want to do is replace the localhost with a connection to my mongo running on my AWS remote server.
I understand, that for security reasons, it is best to ssh tunnel this connection, which I think I am familiar with.
What I have done so far is this:
In a console on the machine hosting the node server (remote to the db) I have run:
ssh -L 8181:127.0.0.1:80 -i ~/LightsailDefaultPrivateKey-eu-west-2.pem bitnami#31.16.56.125 -N
I can then browse to the RockMongo UI from the local machine using localhost:8181/rockmongo ...yay.
If I then run the following:
ssh -L 8181:127.0.0.1:27017 -i ~/LightsailDefaultPrivateKey-eu-west-2.pem bitnami#31.16.56.125 -N
(27017 being the mongo port)
Then try and access the db from my remote machine using:
mongo --username XXXXXX --password XXXXXX 31.16.56.125:8181/testdata
I get the following error:
~]
2017-12-28T22:11:09.791+0000 Error: couldn't connect to server 31.16.56.125:8181 (31.16.56.125), connection attempt failed at src/mongo/shell/mongo.js:148
exception: connect failed
Am I doing this wrong? i.e. is the tunnel only for http connections and not mongo command line use? Do I need to test the connection some other way?
I've Googled all over the place for this and not had much luck (a lot of the AWS docs suggest punching a hole in the firewall - which one can no longer do!)
OK I've (partially) solved this, there were a few things wrong.
1) The mongo client was 2.6 and mongo running on AWS was 3.4. Upgrading this solved some issues - in that I was getting a more meaningful error message.
One thing I did have trouble with is that apt-get seemed to perform an update, yet the version reported when issuing the mongo command was still 2.6.
To solve this I had to run sudo apt-get purge mongodb-org* (note the asterisk). Then perform the update.... If you need to do this then follow these instructions:
https://docs.mongodb.com/v3.2/tutorial/install-mongodb-on-ubuntu/
2) This command
mongo --username XXXXXX --password XXXXXX 31.16.56.125:8181/testdata
won't work as I've omitted 'admin' from it and not specified localhost!
but
mongo admin --username XXXXXX --password XXXXXX localhost:8181/testdata
doesn't work either and gives the following output.
2018-01-03T22:00:42.380+0000 W NETWORK [thread1] Failed to connect to 127.0.0.1:27017, in(checking socket for error after poll), reason: errno:111 Connection refused
2018-01-03T22:00:42.380+0000 E QUERY [thread1] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed :
connect#src/mongo/shell/mongo.js:229:14
#(connect):1:6
The only command I could get to work is:
mongo admin --username XXXXXX --password XXXXXX --port 8181
The default host is localhost, so in this case it uses the tunnel, this will also just connect to the test db, you can then admin from there.
What I haven't got to the bottom of is the specification of the host:port/db as an argument as per the output from running mongo --help
usage: mongo [options] [db address] [file names (ending in .js)]
db address can be:
foo foo database on local machine
192.169.0.5/foo foo database on 192.168.0.5 machine
192.169.0.5:9999/foo foo database on 192.168.0.5 machine on port 9999

sailsJs mongodb connection error in docker container

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.

Docker-Mongodb - How to connect to the mongo image in local(windows)

I am trying to connect to my docker mongo image, I installed mongodb in local, and added the mongodb bin path to windows path environment variable. And I am doing below steps
cd <docker-location>
docker login
docker ps
-- to check already mongo running
docker pull myrepo/mymongo-image:1.0
docker run -p 27017:27017 -d --net=host --name=mytestDB myrepo/mymongo-image:1.0
docker logs mytestDB
Output: MongoDB starting : pid=6 port=27017 dbpath=/data/db 64-bit host=moby
docker ps
Output:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
34598734598 myrepo/mymongo-image:1.0 "/bin/sh -c /usr/bin/" 12 hours ago Up About a minute mytestDB
start mongo in local:
mongo --port 27017
But I am getting error like this:
MongoDB shell version: 3.2.1
connecting to: 127.0.0.1:27017/test
2016-10-13T20:04:12.273+0530 W NETWORK [thread1] Failed to connect to 127.0.0.1:27017, reason: errno:10061 No connection could be made because the target machine actively refused it.
2016-10-13T20:04:12.277+0530 E QUERY [thread1] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed :
connect#src/mongo/shell/mongo.js:226:14
#(connect):1:6
exception: connect failed
Please let me know where I did mistake.
I got solution, I like to share it.
'--noauth --bind_ip=0.0.0.0' options are missed in my commands.
This is the blog bring me out.
Tutorial: Setting up MongoDB Image Instance with Docker Toolbox
if you have already installed docker-toolbox and you have created an image but unable to visualize your mongodb database through robo3t
for this illustration i created my image with command
docker run --name blogA -p 27017:27017 -d mongo
Note: since 27017 is port for mongodb
Open up VirtualBox. Right click the "Default" machine.
Select settings from the menu.
Choose the Network tab in the settings menu.
Choose port forwarding at the bottom of the network option.
Click the plus icon to the right of the port forwarding menu.
Make the new entry with the name mongo.
Protocol should be TCP. Host and Guest IP can be left empty.
Set the host port to the port you make use in creating your
image I made use of port 27017. Set the guest port to 27017.
install robo3t from http://robomongo.org/download.html
click icon below file tab to create a new connection
Choose to create a new connection.
Name your connection and set the address to localhost and port to the port you set your Host port too in VirtualBox, in my case is 27017

Can not connect to Mongo docker instance via mono client on mac

I have installed Mongo docker image and run it using those commands (mac boot2docker is installed)
docker pull mongo
and
docker run --name some-mongo -d mongo
but now I want to connect to it via mongo client running:
mongo --port 27017 --host 127.0.0.1
but I get this error message:
MongoDB shell version: 3.0.4
connecting to: 127.0.0.1:27017/test
2015-07-27T14:22:24.088+0300 W NETWORK Failed to connect to 127.0.0.1:27017, reason: errno:61 Connection refused
2015-07-27T14:22:24.094+0300 E QUERY Error: couldn't connect to server 127.0.0.1:27017 (127.0.0.1), connection attempt failed
at connect (src/mongo/shell/mongo.js:181:14)
at (connect):1:6 at src/mongo/shell/mongo.js:181
exception: connect failed
It is clear to me that Docker fails to expose the ports since telnet to 27017 on the localhost fails as well.
What the hack am I doing wrong?
You have 2 problems :
Like h3nrik said you should connect to the boot2docker VMs address. If you don't know it use the following command :
boot2docker ip
And your port isn't open in the first place.
Your Docker run command should look like this :
docker run -p 27017:27017 --name some-mongo -d mongo
Instead of 127.0.0.1 you should use the boot2docker VMs IP address. Usually 192.168.59.103. You can verify to which IP you should connect executing boot2docker ip.
Update: I discovered that you do not export any ports by your containers run statement:
docker run --name some-mongo -d mongo
Without any ports exposed you cannot connect, of course. Try to re-connect after running (depending on your requirements you can add more ports according to the mongodb documentation):
docker run --name some-mongo -d -p 27017:27017 mongo