Browser Screenshot 我有2个运行mongo:3.0.3和python:2.7的Docker容器。我有一个app.py,它连接到mongodb实例。在我的app.py中,我已使用端口5000映射到27017。app.py运行良好,但是当我访问http://0.0.0.0:5000时,此连接拒绝错误出现在终端上。
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
我找不到任何.conf文件,因为这是一个容器。关于如何解决此问题的任何想法? :/
我的docker映像https://hub.docker.com/r/sizijay/dockerexercise/ 查看dockerfile的详细说明
答案 0 :(得分:1)
运行Mongo容器:
$ docker run -d -p "27017:27017" mongo
在您的 app.py 中,使用 pymongo 连接。
from pymongo import Connection
c = Connection(host=0.0.0.0, port=27017)
c.test_database
注意:主机是 0.0.0.0 。
运行容器:
$ docker run -d -p 5000:5000 sizijay/dockerexercise
如果您使用 localhost 作为主机,则 pymongo 将在运行容器的内部(即 sizijay / dockerexercise )。 当您使用 0.0.0.0 时,连接将连接到您的主机,该主机现在在mongo容器中具有绑定到27017的端口27017。
更好的选择是使用 docker-compose 链接您的容器。
在这种情况下,您需要创建一个文件: docker-compose.yml :
version: '3'
services:
db:
image: mongo
ports:
- "27017:27017"
myapp:
image: sizijay/dockerexercise
ports:
- "5000:5000"
links:
- db
depends_on:
- db
在您的 app.py 中,使用 pymongo 连接。
from pymongo import Connection
c = Connection(host='db', port=27017)
c.test_database
注意:主机是 db 。
现在,主机不在 app 容器中,而是在 db 服务(即mongo容器)中。
您可以通过以下方式运行docker-compose:
$ docker build -t sizijay/dockerexercise .
$ cd location_of_docker-compose.yml
$ docker-compose up
答案 1 :(得分:0)
另一种解决方案-不使用 docker-compose -通过使用--network
中的docker run
标志在同一网桥网络上运行两个容器这两个容器的命令:
docker run -p 27017:27017 --name mongo1 -d --network sizijaynetwork mongo
docker run -p 5000:5000 --name app1 -d --network sizijaynetwork appimage
然后在您的Python脚本中可以执行以下操作:
from pymongo import MongoClient
client = MongoClient(host = 'mongo1', port = 27017)
db = client.databaseName
请注意,主机名应该是您分配给Mongo容器的名称,并且如果不使用--name
标志,只需使用容器的默认名称,您可以在 NAMES下找到当您执行docker ps
答案 2 :(得分:0)
在我使用Mongoengine时,连接参数是
db = connect(<database name>, host='mongo-server', port=27017)
我的docker-compose.yml是
version: "3.4"
services:
...
...
db:
image: mongo:3.4
container_name: mongo-server
ports:
- "27015:27017"
我能够从作为Docker容器运行的Web应用程序连接mongodb。