Docker mongodb 加载 http://0.0.0.0:27017 时 mac os 连接被拒绝

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

Browser Screenshot 我有 2 docker 个容器 运行 mongo:3.0.3 和 python:2.7。我有一个 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/ 查看 docker 文件

的详细说明

运行 Mongo 容器:

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

在你的app.py中,使用pymongo Connection.

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 时,连接将连接到您的 主机 ,它现在已将端口 27017 绑定到 [=86= 中的 27017 ]容器。


更好的选择是使用 docker-compose 到 link 你的容器。

在这种情况下,您需要创建一个文件: 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 Connection.

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

注:主机为db.

现在您的主机不在您的 app 容器内,而是在 db 服务(即 mongo 容器)内.

您可以运行 docker-撰写:

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

另一种解决方案——不使用 docker-compose——是 运行 同一桥接网络上的两个容器(使用docker network create <networkname>) 通过在两个容器的 docker run 命令中使用 --network 标志:

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

因为我使用的是 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"

我能够从我的 Web 应用程序 运行 连接 mongodb 作为 docker 容器。