Unable to connect to mongoDB running in docker container - mongodb

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'

Related

Not able to connect to mongodb running in docker container

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.

mongodb in docker ERR_CONNECTION_TIMED_OUT

Please help me with this. I tried the following steps, but I am not able to connect to MongoDB.
RUN: docker run -p 27017:27017 --name my-mongo -d mongo:latest
RUN: docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' my-mongo
The output of the above command is an IP-Address. Let the IP-Address is 172.17.0.2.
Open in browser this link: http://172.17.0.2:27017/
If the following output is displayed in a browser then everything is fine: It looks like you are trying to access MongoDB over HTTP on the native driver port. I am stuck here.
Thanks
The IP Address you are trying to access is the container's private ip that is only accessible from within the docker network.
Using the parameter -p that you're specifying on the docker run you're telling docker to map a local port on your host to the port specified on the container, in this case local port 27017 to container port 27017 and after that you can access it with localhost: http://localhost:27017
Read more about this here: https://docs.docker.com/config/containers/container-networking/

Connect to container's localhost interface in Docker

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

connect robomongo to mongoDB docker container

I'm running a NodeJS App with docker-compose. Everything works fine and I can see all my data by connecting to Mongo inside container. But when I connect to RoboMongo I don't see any data.
How can I deal with this problem?
There is another way. You can
SSH with Robomongo into your actual virtual server that hosts your docker applications (SSH tab, check "Use SSH tunnel" and complete the other fields accordingly)
Now ssh into the same machine in your terminal.
docker ps should show you your MongoDB container.
docker inspect <mongo container id> will print out complete information about that container. Look for IPAddress in the end, that will give you the local IP of the container.
In the "Connection" tab in Robomongo use that container IP to connect.
Another sidenote: Make sure that you don't expose your mongodb service ports in any way (neither Dockerfile nor docker-compose.yml), cause that will make your database openly accessible from everywhere. Assuming that you don't have set up a username / password for that service you will be scanned and hacked soon.
The easiest way is to enable forwarding the Mongo Container itself, here's how my docker-compose looks like.
mongo:
image: mongo
restart: always
ports:
- 27017:27017
You should do a Robomongo SSH tunnel connection to MongoDB inside docker container. First of all you should install a ssh server inside your docker container.
https://docs.docker.com/engine/examples/running_ssh_service/
After that you should configure your connection in Robomongo.
Inside "Connection Settings" there are configuration tabs of your Robomongo Connection.
Go to "SSH" Tab and configure your SSH connection to the docker container. After that go to "Connection" Tab and configure your connection to MongoDB as if it was in localhost scope.
I was facing a different problem. I had installed MongoDB locally. So, when the MongoDB on docker was running, it was clashing with the one running on my host. I had installed it using brew.
So, I ran
brew services stop mongodb-community
and then I restarted Robo3t. I saw the databases created in the MongoDB running on the docker.
Voila!
Please note that maybe you won't be able to use ssh because it was just a problem of incompatibility between mongo and robomongo.
'Robomongo v8.5 and lower doesn't support MongoDB 3'. It has nothing to do with docker.
First log in with ssh Login details
ssh -i yourpemfile.pem username#ipaddress
Check running container id for MongoDB
docker ps -a
then check the mongo container id
docker inspect container_id
Then open robo3t
create new connection and add container id
use ssh login details to connect to mongodb
In your docker-compose file, you can expose a port to the host.
For example, the following code will expose port 27017 inside the machine to the port 27018 in the host.
app:
image: node
volumes:
- /app
ports:
- "27018:27017"
Then, if you have docker-machine installed and your machine is default, you can do in a terminal :
docker-machine ip default
It will give you the ip of your host, for example 192.168.2.3. The address of your database (host) will be 192.168.2.3 and the port 27018.
If your docker machine is not virtual and is your OS, the address of your database will be localhost and the port 27018.

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