Docker Community Forums

Share and learn in the Docker community.

Connect to mongo in docker

dockerhub
docker

(Rubendario) #1

Hi, i try to connect to my mongodb in docker(dockerhub) from my machine or another container with python(pymongo) i tried with docker ip (172.18.0.X), that in code is: client=MongoClient(‘172.18.0.X’,27017)" but dont work, i use to run (docker run --name mongo -d -p 27017:27017 rubendario/get-mongo --noauth --bind_ip=0.0.0.0). help. THANKS


(Sam) #2

i ran your docker run on my linux machine and then ran mongo and it connected to the remote database just fine.

i connected using the host port

mongo

and also using the container ip and port

mongo 172.17.0.3:27017

both show the same stuff

sam@buildserver:~$ mongo 172.17.0.3:27017
MongoDB shell version: 2.4.9
connecting to: 172.17.0.3:27017/test
Server has startup warnings: 
2018-03-02T23:09:15.472+0000 I STORAGE  [initandlisten] 
2018-03-02T23:09:15.472+0000 I STORAGE  [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2018-03-02T23:09:15.472+0000 I STORAGE  [initandlisten] **          See http://dochub.mongodb.org/core/prodnotes-filesystem
2018-03-02T23:09:17.355+0000 I CONTROL  [initandlisten] 
2018-03-02T23:09:17.355+0000 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2018-03-02T23:09:17.355+0000 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2018-03-02T23:09:17.356+0000 I CONTROL  [initandlisten] 
2018-03-02T23:09:17.356+0000 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2018-03-02T23:09:17.356+0000 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2018-03-02T23:09:17.356+0000 I CONTROL  [initandlisten] 
> show dbs;
admin	0.000030517578125GB
config	0.000011444091796875GB
local	0.000030517578125GB

(Rubendario) #3

Thanks for answering, but is not my solution, im trying connect to container of mongo image, from another container without mongo, only with python, i mean, i only want make a get data with python framework in container a, and i cant access to mongo server in container b.


(Sam) #4

so, the second container, trying to connect to mongo container can only use the direct addressing approach
in my case

mongo 172.17.0.3:27017

just for clarity, you did do docker inspect on the mongo db container id to get its ip address, right?


(Rubendario) #5

Yeah i did, but mongo is working in localhost from the container, when i try mongo 172.17.0.3:27017, dont connect to localhost, conversely connect to container ip. Is there any way to access localhost from outside the container?


(Sam) #6

everywhere is localhost

the docker host localhost is the docker host
the mongo container locahost is 172.17.0.3 (on my machine)
the app container localhost is 172.17.0.4 (on my machine)…

NEVER use ‘localhost’… cause you will have SO many issues trying to figure out what you mean from where…

mongo is working from the container at 172.17.0.3 on my machine. and as I pointed out before from the docker HOST there are two ways to get to the container…

from ANOTHER container, you do NOT come back to the docker host to go to the mongo container, you go direct

I started another container (ubuntu), installed the mongo client and

root@d7c5ad369d2a:/# mongo 172.17.0.3:27017
MongoDB shell version: 3.2.19
connecting to: 172.17.0.3:27017/test
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
	http://docs.mongodb.org/
Questions? Try the support group
	http://groups.google.com/group/mongodb-user
Server has startup warnings: 
2018-03-03T17:12:42.203+0000 I STORAGE  [initandlisten] 
2018-03-03T17:12:42.203+0000 I STORAGE  [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2018-03-03T17:12:42.203+0000 I STORAGE  [initandlisten] **          See http://dochub.mongodb.org/core/prodnotes-filesystem
2018-03-03T17:12:44.093+0000 I CONTROL  [initandlisten] 
2018-03-03T17:12:44.093+0000 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2018-03-03T17:12:44.093+0000 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2018-03-03T17:12:44.093+0000 I CONTROL  [initandlisten] 
2018-03-03T17:12:44.093+0000 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2018-03-03T17:12:44.093+0000 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2018-03-03T17:12:44.093+0000 I CONTROL  [initandlisten] 
> show dbs;
admin   0.000GB
config  0.000GB
local   0.000GB
> 

worked just as expected


(Rubendario) #8

I was misunderstanding the concept, but now I have it much clearer, thank you very much