Docker mongodb connection refused on mac os when loading http://0.0.0.0:27017


Docker mongodb connection refused on mac os when loading http://0.0.0.0:27017



Browser Screenshot
I have 2 docker containers running mongo:3.0.3 and python:2.7. And I have an app.py which connects to mongodb instance. In my app.py I have used port 5000 to map 27017. The app.py runs fine but when I visit http://0.0.0.0:5000 this connection refused error comes on terminal.


172.17.0.1 - - [01/Jul/2018 10:01:32] "GET / HTTP/1.1" 500 -
Traceback (most recent call last):
File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/pythonApp/app.py", line 16, in form
if posts.count()!=0:
File "/usr/local/lib/python2.7/site-packages/pymongo/collection.py", line 1023, in count
return self._count(cmd)
File "/usr/local/lib/python2.7/site-packages/pymongo/collection.py", line 985, in _count
with self._socket_for_reads() as (sock_info, slave_ok):
File "/usr/local/lib/python2.7/contextlib.py", line 17, in __enter__
return self.gen.next()
File "/usr/local/lib/python2.7/site-packages/pymongo/mongo_client.py", line 699, in _socket_for_reads
with self._get_socket(read_preference) as sock_info:
File "/usr/local/lib/python2.7/contextlib.py", line 17, in __enter__
return self.gen.next()
File "/usr/local/lib/python2.7/site-packages/pymongo/mongo_client.py", line 663, in _get_socket
server = self._get_topology().select_server(selector)
File "/usr/local/lib/python2.7/site-packages/pymongo/topology.py", line 121, in select_server
address))
File "/usr/local/lib/python2.7/site-packages/pymongo/topology.py", line 97, in select_servers
self._error_message(selector))
ServerSelectionTimeoutError: localhost:27017: [Errno 111] Connection refused



I can't find any .conf files as this is a container. Any idea on how to get this fixed? :/



my docker image https://hub.docker.com/r/sizijay/dockerexercise/
see the long description for dockerfile





Just visit the ip Adress of the container.
– Pils19
Jul 1 at 10:17




1 Answer
1



Run the Mongo container:


$ docker run -d -p "27017:27017" mongo



In your app.py, use pymongo Connection.


from pymongo import Connection
c = Connection(host=0.0.0.0, port=27017)
c.test_database



Note: the host is 0.0.0.0.



Run your container:


$ docker run -d -p 5000:5000 sizijay/dockerexercise



If you use localhost as your host, pymongo will be looking inside the container from which is running (i.e. sizijay/dockerexercise).
When you use 0.0.0.0, the connection will be to your host machine, which now has port 27017 bound to 27017 inside your mongo container.



A better alternative would be to use docker-compose to link your containers.



In this case, you need to create a file:
docker-compose.yml:


version: '3'

services:
db:
image: mongo
ports:
- "27017:27017"

myapp:
image: sizijay/dockerexercise
ports:
- "5000:5000"
links:
- db
depends_on:
- db



In your app.py, use pymongo Connection.


from pymongo import Connection
c = Connection(host='db', port=27017)
c.test_database



Note: the host is db.



Now your host is not inside your app container, but inside the db service (i.e. the mongo container).



You can run docker-compose by:


$ docker build -t sizijay/dockerexercise .
$ cd location_of_docker-compose.yml
$ docker-compose up





mongo is already running 0.0.0.0:27017->27017/tcp and when I visit 0.0.0.0:27017 in browser there's an echo "It looks like you are trying to access MongoDB over HTTP on the native driver port." so the problem is with app.py it can't connect
– sizijay
Jul 1 at 12:25






the 2 containers are already running mongo-> 0.0.0.0:27017->27017/tcp sizijay/dockerexercise -> 0.0.0.0:5000->5000/tcp
– sizijay
Jul 1 at 12:31





@sizijay, Just because you exposed it on host doesn't mean it would work. When you use localhost:27017 inside the app.py it is running inside the container in which only 5000 is the port which has something. So you test on host and say 27017 is accessible on localhost but that localhost is in context with your machine and not with respect to container
– Tarun Lalwani
Jul 1 at 13:36


localhost:27017


app.py


5000


27017





@TarunLalwani yeah. I understand now. the app.py is working fine when I run it without docker. so the problem is with linking the containers? right?
– sizijay
Jul 1 at 14:10





Yes, you need to use the container name or better launch the whole thing using a docker-compose config
– Tarun Lalwani
Jul 1 at 14:36


docker-compose






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

List of Kim Possible characters

Audio Livestreaming with Python & Flask

NSwag: Generate C# Client from multiple Versions of an API