testcontainers-python icon indicating copy to clipboard operation
testcontainers-python copied to clipboard

MongoDbContainer on Windows 10 fails with getaddrinfo error

Open arpost opened this issue 5 years ago • 3 comments

The following code fails with testcontainers-python 3.0.2:

from testcontainers.mongodb import MongoDbContainer
with MongoDbContainer('mongo:4.2.2') as mongo_:
...     db = mongo_.get_connection_client().test
...     db.test.insert_one({'foo': 'bar'})

with

Pulling image mongo:4.2.2
Container started:  d5ca66c443
Traceback (most recent call last):
  File "<input>", line 3, in <module>
  File "C:\...\venv\lib\site-packages\pymongo\collection.py", line 695, in insert_one
    self._insert(document,
  File "C:\...\venv\lib\site-packages\pymongo\collection.py", line 610, in _insert
    return self._insert_one(
  File "C:\...\venv\lib\site-packages\pymongo\collection.py", line 599, in _insert_one
    self.__database.client._retryable_write(
  File "C:\...\venv\lib\site-packages\pymongo\mongo_client.py", line 1490, in _retryable_write
    with self._tmp_session(session) as s:
  File "C:\Program Files\Python38\lib\contextlib.py", line 113, in __enter__
    return next(self.gen)
  File "C:\...\venv\lib\site-packages\pymongo\mongo_client.py", line 1823, in _tmp_session
    s = self._ensure_session(session)
  File "C:\...\venv\lib\site-packages\pymongo\mongo_client.py", line 1810, in _ensure_session
    return self.__start_session(True, causal_consistency=False)
  File "C:\...\venv\lib\site-packages\pymongo\mongo_client.py", line 1763, in __start_session
    server_session = self._get_server_session()
  File "C:\...\venv\lib\site-packages\pymongo\mongo_client.py", line 1796, in _get_server_session
    return self._topology.get_server_session()
  File "C:\...\venv\lib\site-packages\pymongo\topology.py", line 482, in get_server_session
    self._select_servers_loop(
  File "C:\...\venv\lib\site-packages\pymongo\topology.py", line 208, in _select_servers_loop
    raise ServerSelectionTimeoutError(
pymongo.errors.ServerSelectionTimeoutError: localnpipe:32775: [Errno 11001] getaddrinfo failed

Interestingly, the MongoDbContainer.get_connection_url() method returns a URL with hostname localnpipe, which I suspect the mongo client does not understand. Using pymongo directly, I can connect to the created container using a hostname of localhost.

arpost avatar Jun 07 '20 02:06 arpost

@arpost Thanks for reporting. I suspect localnpipe is what docker client resolves internally. Since, test cases of this project does not cover Windows OS, I will test it on Windows machine soon. Meanwhile, possible workaround is that you can use TC_HOST env variable to set the ip address of the docker host. So that get_connection_url() returns with specified host name.

Also, can you test with PR #96 and share the result if possible?

tuvshuud avatar Jun 08 '20 04:06 tuvshuud

I tested with PR #96 and it does not address my issue, unfortunately. I believe the problem is in core/docker_client.py, and I'll submit a pull request soon.

arpost avatar Jun 08 '20 19:06 arpost

#100

alexanderankin avatar Mar 08 '24 08:03 alexanderankin