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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With