Can not connect to Mongo docker instance via mono client on mac - 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?

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

Related

Connect to Mongo Router inside Docker container from remote host

I have an EC2 instance in AWS with a mongo docker container that init a mongo router such as :
docker run --network mongo-net -p 27051:27017 --expose=27017 -d --name=mongos51 mongo mongos --port 27017 --configdb configserver/configserver41:27017,configserver42:27017 --bind_ip 0.0.0.0
I have opened my SG in AWS to allow any connections and i can reach the public IP on the port 27051 without problems.
My container is in a bidge docker network to allow all of my mongo instances to communicate.
However, if i try to connect to the mongo shell like :
mongosh mongodb://public_ip:27051
I have the error Connection refused instantly. Same if i try to open the mongoshell from inside the container with :
docker exec -it mongos51 bash -c "echo 'sh.addShard(\"shard1/mongodb11\")' | mongosh "
Current Mongosh Log ID: 6149ed68def89bc1907c8988
Connecting to: mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000
MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017
I have the same error if i use the private IP instead of 127.0.0.1.
What i am missing here ?
According the documentation try to connect like this :
mongosh --host public_ip --port 27051
It work for me when I follow these steps.

Connect to MongoDB query router for Sharding on a docker container running on windows10

This is a follow up of my previous question. Alex Blex's solution for connecting to the config servers works great. But I am facing the same issue while connecting to the MongoDB Query router.
Below is the command I am using to create the mongos server
docker run -d -p 40001:27017 -v C:/mongodata/data/db --name QR mongo mongos --configdb rs1/172.30.35.165:30001,172.30.32.73:30002,172.30.42.189:30003 --bind_ip 0.0.0.0 --port 27017
But I get the below error on executing docker exec -it QR mongo -port 27017:-
connecting to:
mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Error: couldn't connect to server 127.0.0.1:27017, connection attempt
failed: SocketException: Error connecting to 127.0.0.1:27017 :: caused
by :: No connection could be ma de because the target machine actively
refused it. : connect#src/mongo/shell/mongo.js:374:17 #(connect):2:6
exception: connect failed exiting with code 1
Below is the replication configuration details for the Config Servers -
config = {
"_id": "rs1",
"configsvr": true,
"members":
[
{
"_id": 0,
"host": "6ed1d953f979:27019"
},
{
"_id": 1,
"host": "086f0ef5c955:27019"
},
{
"_id": 2,
"host": "391c9c07b341:27019"
}
]
}
Here is the container ID and IP address
------------------------------------------
Server IP Address Container ID
------------------------------------------
asiaCS 172.30.35.165 6ed1d953f979
europeCS 172.30.32.73 086f0ef5c955
americaCS 172.30.42.189 391c9c07b341
I am not sure if I am even configuring the mongos properly.
So I figured this one out. Apparently config servers are light weight and do not store any data. Hence, we do not require to bind it to a volume. I first bound all the config servers to a fixed IP (so that docker doesn't assign them a new IP every time I stop and start a container). But for the sake of this answer, I will be using the IPs mentioned in the question itself. I used the below command to create a query router.
docker run -d -p 40001:27017 --name QR --hostname QR mongo mongos --configdb "rs1/172.30.35.165:30001,172.30.32.73:30002,172.30.42.189:30003" --bind_ip 0.0.0.0
Then docker exec'd into the container by running docker exec -it QR mongo
Now while connecting to mongos, if it throws a connection refused error (the one mentioned in the question), you could use the following command -
docker exec -it QR mongos --configdb "rs1/172.30.35.165:30001,172.30.32.73:30002,172.30.42.189:30003"
The above command will start the mongos without the detached mode and logs will start appearing on your CMD or PowerShell (whichever way you are running the command) console.
I have mongo shell on my local and I opened another CMD prompt and executed the below command -
C:\MongoShell\bin> mongosh --host IpAddressOfMongos --port 27017
And voila, it connected successfully. You can then close the initial console where you were running docker exec command in attached mode.
IP Address of the mongos can be found out by doing docker inspect QR.

How to run girder on docker container connecting to mongodb

How do I run Girder on Docker container hooking it up to MongoDB? Both are in their own Docker containers.
I have pulled and started a MongoDB container.
docker run --name mongodb_girder -e MYSQL_ROOT_PASSWORD=SuperSecretPWD -d mmongo:latest
But when I tried running Girder on another Docker container, I was unable to get it up and running.
docker run -p 81:8080 girder/girder -d mongodb://localhost:27017/mongodb_girder --host 0.0.0.0
Here is the error I get:
pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [Errno 111] Connection refused
Did you try changing the host IP to 127.0.0.1? This is the IP for the local system, which is the one used by your MongoDB.
0.0.0.0 is for all IPs of a given system and is non-routable.
Try to change your host by : host.docker.internal

Mongo docker image - unable to run on different port

There are a tons of question here on SO citing how this command alone
docker run --name mymongo --network bridge -p 27117:27117 -v "$PWD/db":/data/db -d mongo
should run mongo on port 27117.
However this doesn't work for me. The container runs, but the mongo is run on its default port alone (see output from container itself):
# mongo
MongoDB shell version v4.0.4
connecting to: mongodb://127.0.0.1:27017
# mongo --port 27117
MongoDB shell version v4.0.4
connecting to: mongodb://127.0.0.1:27117/
2018-11-20T17:26:09.345+0000 E QUERY [js] Error: couldn't connect to server 127.0.0.1:27117, connection attempt failed: SocketException: Error connecting to 127.0.0.1:27117 :: caused by :: Connection refused :
What is going on?
Thanks a lot!
Inside your container mongo runs on its default port, which is 27017
So, you should modify your command and specify the port mapping like this: -p 27117:27017
The full command would be this:
docker run --name mymongo --network bridge -p 27117:27017 -v "$PWD/db":/data/db -d mongo
With that command you are telling docker that the port is 27117, but you also need to start mongo with that port.
To do so, just add --port 27117 at the end of your command:
docker run --name mymongo --network bridge -p 27117:27117 -v "$PWD/db":/data/db -d mongo --port 27117

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