mongodb in docker ERR_CONNECTION_TIMED_OUT - mongodb

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/

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.

I want to connect my MongoDB docker to my host pc

I want to connect my MongoDB docker with my program in my host.
I try this:
docker pull mongo
docker run -d --name mongodb -p 21017:21017 mongo
docker exec -it mongodb bash
All start fine but I couldn't connect to my host, I try to change my archive /etc/mongod.conf but with anything result.
I have a python program in my host and I want to use docker MongoDB and connect both.
docker container ls
Thank you very much.
That isnt the mongo port 21017:
Mongo port is 27017 .
You need to use below host & port in your python program which resides on docker host -
DB_HOST = localhost
DB_PORT = 27017
From your Docker host, mongoDB container should be accessible at localhost: 27017
Update 1(as suggested by #Schwarz54) -
Also, you are using wrong mongo port, it should be 27017.
Run your container using below command -
docker run -d --name mongodb -p 27017:27017 mongo
I find the problem, it was that in my python program I don't remember to import mongo.
Yes, that was the problem.
Now I can:
myclient = pymongo.MongoClient("mongodb://192.168.10.170:55059")
And all fine.
Thank you for the help I stay two days checking all the code but I don't remember to see if I import the library...

Docker port mismatch from inside another container

I have a simple setup of 2 docker containers, one for the database and one for the web service.
I start the DB docker container like so:
docker run -d --name dbs.service -p 5434:5432 -e POSTGRES_DB=my_app -e POSTGRES_USER=my_user -e POSTGRES_PASSWORD=my_password postgres:9.6.2
This works fine. And from localhost, i can connect to it fine as well (using pgcli for connection)
pgcli postgres://my_user:my_password#dbs.service:5434/my_app
Now I start the web service container, which works fine
docker run -d --name web.service --link dbs.service:dbs.service web-service:latest
However here's the problem. From inside the container, I cannot connect to DB using port 5434 but I can connect to DB using port 5432.
So I login to container using
docker exec -it web.service bash
Now this works
pgcli postgres://my_user:my_password#dbs.service:5432/my_app
but this does not
pgcli postgres://my_user:my_password#dbs.service:5434/my_app
I can't understand why it can connect to 5432 but not 5434. Any suggestions?
-p 5434:5432
This option publishes the port for access from outside of the docker host to your container. The host will listen on 5434 and route the traffic through to the container's port 5432.
However, container-to-container traffic doesn't use that. Container to container traffic simply needs a common docker network. From there, any container can talk to any other container on the same network. The port used is the container listening port, not the published port on the host. You don't even need to publish the port for it to be accessible by other containers.

'--link' does not seem to work to connect two Docker containers

I would like to run MongoDB in a container, this works:
docker run -p 27017:27017 --name cdt -d mongo
then I want to run a server in another container, like so:
docker run --name foo --link cdt:mongo exec /bin/bash -c "node server.js"
The node.js server attempts to make a mongodb connection to localhost:27017, but it fails to make the connection.
Anyone know why that might happen? Am I not linking the containers correctly?
Note that I can successfully connect to the mongodb container from outside a container, but not from the server inside the "foo" container.
So localhost from a container is always (99.5% of the time) referring to the container itself. This is also 99.5% of the time not what you want. If you're using links like this, you need to change localhost:27017 to mongo:27017 as that's what you're 'mounting' the link as (--link cdt:mongo).
A better option is to use Docker networks instead of links (which are deprecated). So:
$ docker network create my-net
$ docker run --name cdt --net my-net -d mongo
$ docker run --name foo --net my-net exec /bin/bash -c "node server.js"
Now you'd refer to your db via cdt:27017 as the names of the containers become resolvable via DNS on the same network. Note that you don't need to expose a port if you're not intending to connect from the outside world, inter-connectivity between containers on the same network doesn't require port mapping.

Unable to connect to mongoDB running in docker container

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'