As part as an attempt to resolve a Hibernate OGM connection issue, I want to see if I connect from Robo 3T.
I build my MongoDB image and start it running.
docker ps:
MacBook-Pro:GoStopHandle NOTiFY$ docker ps CONTAINER ID IMAGE
COMMAND CREATED STATUS PORTS
NAMES 0375a68b9988 gostophandlemongodb:latest
"docker-entrypoint.s…" 5 seconds ago Up 4 seconds
0.0.0.0:32844->27017/tcp goStopHandleMongo
The IP address of the containers is:
docker inspect -f '{{range
.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' 0375a68b9988
172.17.0.2
I've enter the 'mongo' shell:
docker exec -it goStopHandleMongo mongo admin
I add a user & password:
> db.createUser(
... {
... user: "NOTiFY",
... pwd: "MyPassword",
... roles: [ { role: "userAdminAnyDatabase", db: "admin" }, "readWriteAnyDatabase" ]
... }
... )
Successfully added user: {
"user" : "NOTiFY",
"roles" : [
{
"role" : "userAdminAnyDatabase",
"db" : "admin"
},
"readWriteAnyDatabase"
]
}
I then create a connection in Robo 3T:
I then set-up the 'Authentication':
When I try & connect I get:
Any suggestions?
It wasn't until I had to reboot my Mac that I realised that I'd still had a local instance of MongoDB running, therefore the I wasn't connecting to my Docker instance of MongoDB but my local (Brew installed) instance.
Once I'd seen #Artjom Zabelin answer here and "Don't forget to map port to host port"
docker run --name some-mongo -p 27017:27017 -d mongo
It connected to MongoDB running my container.
#Frank Yucheng Gu was correct that I needed 'localhost'.
To connect, you should simply connect with localhost:27017 on Robo3T.
The problem originates in the confusion between a container's internal IP and its external IP. The internal IP is your container's address in the docker network, which is isolated from you host network (unless explicitly bridged). When you do the docker inspect command, you grabbed the container's IP on the docker network.
Your container is listening on 0.0.0.0:32844 in the docker network, and exposed to the host with a port mapping to port 27017. So if you have another container in the docker network, you should access your service with 172.17.0.2:32844. But if you have anything outside of the docker network, you should access your mongodb with either localhost:27017 or [your host IP]:27017.
Hope this helps!
In my case, I was running docker on Mac OSX and docker was using my host IP address, and as a result docker inspect CONTAINER_ID --format '{{ .NetworkSettings.IPAddress }}' would give me an empty string. Docker host address on Mac OSX and Windows is host.docker.internal. So I was able to connect to the mongodb instance running in docker after setting the host address on robo 3t to host.docker.internal
Related
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.
I have a mongoDB DB container running and I'am not able to connect to it using Robo 3T, I get the folowing error.
Any help on what am I missing here.
Look at the PORTS column of the docker ps output :
PORTS
27017/tcp
No mapping is specified (-> symbol). It means that the container port is not published outside the docker networks.
Two possible ways to solve your issue :
Don't specify localhost as hostname but the container ip : you can retrieve it with docker inspect CONTAINER. If you use cygwin : docker inspect CONTAINER | grep IPAddress.
publish the mongo container port on the host machine. You can publish it on the same port or not with the -p arg such as -p HOST_PORT:CONTAINER_PORT.
For example with the same port : docker run -d -p 27017:27017 mongo:latest.
You could see with docker ps that the port is published on the host now :
PORTS
0.0.0.0:27017->27017/tcp
Bind mongodb port to your docker host to access it using localhost from the host -
docker run -d -p 27017:27017 ....
I suspect you are missing -p 27017:27017.
Is it possible to connect to a process running in a Docker container but exactly via container's loopback interface ?
Basically I am looking for this option:
docker run ... -p 12345:127.0.0.1:12345 ...
This is rejected by Docker.
The use case is for example to setup Mongo admin user via Localhost Exception
You have the port mapping slightly wrong. Try it like this:
$ docker run -itd -p 27117:27017 mongo
so 27117 is the port you can connect to outwith the docker container, and 27017 is the port mongod is running on within docker
So when I connect the mongo shell I can connect to the mongod within docker like:
$ mongo --port 27117
Try like this :
Step 1 :
pulling mongo image from docker.
docker pull mongo
Step 2 :
Create new directory for storing mongodb data in you shared location like /home/user/databases/mongo
Step 3:
Run mongodb from docker image using below command
if you want change the mongodb port like 12345 set it in the command. 27017 is globally declared in docker you cannot change it but i want locally change the mongodb port like 12345
docker run -d -p 12345:27017 -v /home/user/databases/mongo:/data/db --name mongodb mongo
I am using the official mongodb docker container.
I want to connect to the mongodb container from my host machine on port 27017.
I ran the container with these ports exposed
-p 27017:27017
I am not able to connect (connection refused) and I believe its because the mongo conf file is not configured to allow remote connections. How can I configure it to allow? The official container does not have vi/nano installed to modify the image.
I am able to connect to mongodb from another container by creating a link - however this is not my wish
Better solutions for furthering:
https://blog.madisonhub.org/setting-up-a-mongodb-server-with-auth-on-docker/
https://docs.mongodb.com/v2.6/tutorial/add-user-administrator/
My answer to another question. How to enable authentication on MongoDB through Docker?
Here's what I did for the same problem, and it worked.
Run the mongo docker instance on your server
docker run -d -p 27017:27017 -v ~/dataMongo:/data/db mongo
Open bash on the running docker instance.
docker ps
CONTAINER IDIMAGE COMMAND CREATED STATUS PORTS NAMES
b07599e429fb mongo "docker-entrypoint..." 35 minutes ago Up 35 minutes 0.0.0.0:27017->27017/tcp musing_stallman
docker exec -it b07599e429fb bash
root#b07599e429fb:/#
Reference- https://github.com/arunoda/meteor-up-legacy/wiki/Accessing-the-running-Mongodb-docker-container-from-command-line-on-EC2
Enter the mongo shell by typing mongo.
root#b07599e429fb:/# mongo
For this example, I will set up a user named ian and give that user read & write access to the cool_db database.
> use cool_db
> db.createUser({
user: 'ian',
pwd: 'secretPassword',
roles: [{ role: 'readWrite', db:'cool_db'}]
})
Reference: https://ianlondon.github.io/blog/mongodb-auth/ (First point only)
Exit from mongod shell and bash.
Now run the mongo docker with auth enabled.
docker run -d -p 27017:27017 -v ~/dataMongo:/data/db mongo mongod --auth
Reference: How to enable authentication on MongoDB through Docker? (Usman Ismail's answer to this question)
I was able to connect to the instance running on a Google Cloud server from my local windows laptop using the below command.
mongo <ip>:27017/cool_db -u ian -p secretPassword
Reference: how can I connect to a remote mongo server from Mac OS terminal
Following this example: https://docs.docker.com/engine/examples/mongodb/
When trying to connect to mongoDB with: mongo ip:27017
(where ip is the name from boot2docker ip) + the port number from docker ps:
27017/tcp
or with -P
0.0.0.0:49155->27017/tcp
Either way I get the following errors:
warning: Failed to connect to ip:27017, reason: errno:61 Connection
refused
Error: couldn't connect to server ip:27017 (ip), connection attempt
failed at src/mongo/shell/mongo.js:148 exception: connect failed
If you specified the correct port and still not able to connect to mongodb running in docker (like me), make sure you are using the service name (or container name) in your connection URL, e.g. mongodb://mongodb_service:27017/mydb, which is defined in your docker-compose.yml :
services:
mongodb_service:
image: mongo
I was using the hostname value and that's not the correct thing to do. You could verify this by looking at docker inspect mongodb_service in the Aliases section.
I was using port 27017 instead of 49155 (doh, port forwarding)
0.0.0.0:49155->27017/tcp
Thanks to ZeissS
If you are on a Mac and using Docker Machine, do the following:
1. Get the name of the VM running docker daemon
$ docker-machine ls
2. Get the VM's IP info
$ docker-machine env
3. Connect with the mongo client to the VM IP and the mongo mapped port
$ mongo VM-IP:port
Assuming your mongodb is within a container, for other containers to connect to it, they all need to be on the same network.
To have mongodb and other containers (that want to connect it), create a new network using below command
docker network create --driver bridge my_bridge
Then run mongodb and other containers using the --net flag
docker run --net=my_bridge --name mongodb -p 27017:27017 mongodb
docker run --net=my_bridge --name my-service -p 7002:7002 my-service
Now you should be able to connect mongodb with given alias name from those containers
mongo --host "mongodb:27017"
DATABASE_URI=mongodb://mongo:27017/db_name
Should be the Database URI for a service definition like below (and not mongodb://localhost
or mongodb://IP). Use service name or container name.
services
mongo:
container_name: mongo
image: mongo
ports:
- '27017:27017'