无法从另一个容器连接到mongodb docker容器

4

我有以下简化的设计:一个mongodb容器和一个“python-client”docker容器,它链接到前者。这是我的简化版docker-compose.yml文件:

mongodb:
  build: "mongodb"
  dockerfile: "Dockerfile"
  hostname: "mongodb.local"
  ports:
  - "27017:27017"

client:
  build: "client"
  dockerfile: "Dockerfile"
  hostname: "client.local"
  links:
  - "mongodb:mongodb"
  environment:
  - "MONGODB_URL=mongodb://admin:admin@mongodb:27017/admin"
  - "MONGODB_DB=historictraffic"

我能够使用 pymongo 从我的主机成功连接到 MongoDB 数据库,连接字符串为 mongodb://admin:admin@localhost:27017/admin(注意 localhost):

$ ipython
from pymongo import MongoClient
mongo = MongoClient('mongodb://admin:admin@localhost:27017/admin')
db = mongo.test
col = db.test
col.insert_one({'x': 1})
# This works

但是我无法从客户端容器连接。 显然链接是正确的:

 / # cat /etc/hosts
172.17.0.27     client.local client
127.0.0.1       localhost
::1     localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.26     historictraffic_mongodb_1 mongodb
172.17.0.26     mongodb mongodb historictraffic_mongodb_1
172.17.0.26     mongodb_1 mongodb historictraffic_mongodb_1

但是当我进行同样的测试时,它失败了:
/ # ipython
from pymongo import MongoClient
mongo = MongoClient('mongodb://admin:admin@mongodb:27017/admin')
db = mongo.test
col = db.test
col.insert_one({'x': 2})
---------------------------------------------------------------------------
ServerSelectionTimeoutError               Traceback (most recent call last)
<ipython-input-5-c5d62e5590d5> in <module>()
----> 1 col.insert_one({'x': 2})

/usr/lib/python2.7/site-packages/pymongo/collection.pyc in insert_one(self, document)
    464         if "_id" not in document:
    465             document["_id"] = ObjectId()
--> 466         with self._socket_for_writes() as sock_info:
    467             return InsertOneResult(self._insert(sock_info, document),
    468                                    self.write_concern.acknowledged)

/usr/lib/python2.7/contextlib.pyc in __enter__(self)
     15     def __enter__(self):
     16         try:
---> 17             return self.gen.next()
     18         except StopIteration:
     19             raise RuntimeError("generator didn't yield")

/usr/lib/python2.7/site-packages/pymongo/mongo_client.pyc in _get_socket(self, selector)
    661     @contextlib.contextmanager
    662     def _get_socket(self, selector):
--> 663         server = self._get_topology().select_server(selector)
    664         try:
    665             with server.get_socket(self.__all_credentials) as sock_info:

/usr/lib/python2.7/site-packages/pymongo/topology.pyc in select_server(self, selector, server_selection_timeout, address)
    119         return random.choice(self.select_servers(selector,
    120                                                  server_selection_timeout,
--> 121                                                  address))
    122 
    123     def select_server_by_address(self, address,

/usr/lib/python2.7/site-packages/pymongo/topology.pyc in select_servers(self, selector, server_selection_timeout, address)
     95                 if server_timeout == 0 or now > end_time:
     96                     raise ServerSelectionTimeoutError(
---> 97                         self._error_message(selector))
     98 
     99                 self._ensure_opened()

ServerSelectionTimeoutError: mongodb:27017: [Errno 113] Host is unreachable

有人知道如何解决这个问题吗?谢谢。


我遇到了完全相同的问题... - thearrow3456
1个回答

0

这个错误可能是因为Mongo还没有启动。您可以在重试之间设置短暂的延迟,经过一两次尝试后,连接应该可以正常工作。


嗨@dnephin,我尝试了几次都没有成功,即使过了几分钟。Mongo已经完全启动,因为我可以从我的主机(使用“localhost”)连接到它。 - borges

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接