Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

docker

mongodb

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?

like image 657
moshe beeri Avatar asked Jan 08 '23 21:01

moshe beeri


2 Answers

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

like image 77
Regan Avatar answered Jan 17 '23 23:01

Regan


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
like image 20
Henrik Sachse Avatar answered Jan 17 '23 23:01

Henrik Sachse