Official atlas docker image only accepts connections on localhost

We are trying to integrate the atlas docker image into our ci flow for our tests. We are using atlas because we need our tests to include database triggers, so using a basic mongo image is not possible. Here is the official documentation:

When we attempt to connect with the container using mapped ports via localhost, all goes well. A problem arises when the image is part of a docker network and referenced as a service name. For example, instead of mongodb://localhost in a docker network one would use mongodb://mongodb (assuming one has named the service mongodb)

Here is the stacktrace:
Traceback (most recent call last):
File “/usr/local/bin/beanie”, line 8, in
sys.exit(migrations())
^^^^^^^^^^^^
File “/usr/local/lib/python3.11/site-packages/click/core.py”, line 1157, in call
return self.main(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/usr/local/lib/python3.11/site-packages/click/core.py”, line 1078, in main
rv = self.invoke(ctx)
^^^^^^^^^^^^^^^^
File “/usr/local/lib/python3.11/site-packages/click/core.py”, line 1688, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/usr/local/lib/python3.11/site-packages/click/core.py”, line 1434, in invoke
return ctx.invoke(self.callback, **ctx.params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/usr/local/lib/python3.11/site-packages/click/core.py”, line 783, in invoke
return __callback(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/usr/local/lib/python3.11/site-packages/beanie/executors/migrate.py”, line 200, in migrate
asyncio.run(run_migrate(settings))
File “/usr/local/lib/python3.11/asyncio/runners.py”, line 190, in run
return runner.run(main)
^^^^^^^^^^^^^^^^
File “/usr/local/lib/python3.11/asyncio/runners.py”, line 118, in run
return self._loop.run_until_complete(task)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/usr/local/lib/python3.11/asyncio/base_events.py”, line 654, in run_until_complete
return future.result()
^^^^^^^^^^^^^^^
File “/usr/local/lib/python3.11/site-packages/beanie/executors/migrate.py”, line 110, in run_migrate
root = await MigrationNode.build(settings.path)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/usr/local/lib/python3.11/site-packages/beanie/migrations/runner.py”, line 211, in build
await init_beanie(
File “/usr/local/lib/python3.11/site-packages/beanie/odm/utils/init.py”, line 750, in init_beanie
await Initializer(
File “/usr/local/lib/python3.11/site-packages/beanie/odm/utils/init.py”, line 128, in await
yield from self.init_class(model).await()
File “/usr/local/lib/python3.11/site-packages/beanie/odm/utils/init.py”, line 713, in init_class
await self.init_document(cls)
File “/usr/local/lib/python3.11/site-packages/beanie/odm/utils/init.py”, line 545, in init_document
build_info = await self.database.command({“buildInfo”: 1})
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/usr/local/lib/python3.11/concurrent/futures/thread.py”, line 58, in run
result = self.fn(*self.args, **self.kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/usr/local/lib/python3.11/site-packages/pymongo/_csot.py”, line 105, in csot_wrapper
return func(self, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/usr/local/lib/python3.11/site-packages/pymongo/database.py”, line 805, in command
with self.__client._socket_for_reads(read_preference, session) as (
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/usr/local/lib/python3.11/site-packages/pymongo/mongo_client.py”, line 1296, in _socket_for_reads
server = self._select_server(read_preference, session)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/usr/local/lib/python3.11/site-packages/pymongo/mongo_client.py”, line 1257, in _select_server
server = topology.select_server(server_selector)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/usr/local/lib/python3.11/site-packages/pymongo/topology.py”, line 272, in select_server
server = self._select_server(selector, server_selection_timeout, address)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/usr/local/lib/python3.11/site-packages/pymongo/topology.py”, line 261, in _select_server
servers = self.select_servers(selector, server_selection_timeout, address)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/usr/local/lib/python3.11/site-packages/pymongo/topology.py”, line 223, in select_servers
server_descriptions = self._select_servers_loop(selector, server_timeout, address)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/usr/local/lib/python3.11/site-packages/pymongo/topology.py”, line 238, in _select_servers_loop
raise ServerSelectionTimeoutError(
pymongo.errors.ServerSelectionTimeoutError: Could not reach any servers in [(‘localhost’, 27017)]. Replica set is configured with internal hostnames or IPs?, Timeout: 30s, Topology Description: <TopologyDescription id: 667b0095f99b88ebb79a59a0, topology_type: ReplicaSetNoPrimary, servers: [<ServerDescription (‘localhost’, 27017) server_type: Unknown, rtt: None, error=AutoReconnect(‘localhost:27017: [Errno 111] Connection refused’)>]>

This trace doesn’t make a lot of sense as we make no attempt to connect via localhost. Does anyone have an example of using the atlas image in a larger docker compose?

Hi @Isaac_Martin,

Could you share the docker instance configuration of your local atlas?
Providing the atlas image name in the URI should be the trigger, also consider using only the service name in your client string (I.e. SERVICE_NAME=mongodb, so CONNECTION_STRING=“mongodb”). Additionally, could you provide the code snippet where you instantiate the client?

I have a suspicion, that there is an underlying part of the implemented code that is making a MongoClient() call with no passed URI, that will automatically look for localhost:27017.

As an additional test, can you place a snippet in your code that does this before anything else boots:

from pymongo import MongoClient

client = MongoClient(CONNECTION_STRING)
client.admin.command('ping')

and check if it raises any error. If it does not raise an error that confirms my earlier suspicion. Otherwise, that means there may be another issue with the atlas configurations service or that the docker image isn’t running.

Hope this helps!

We also tried changing the mongo.conf file, as it seems to be set to only accept connections from 127.0.0.1; but that had no affect.

Hello @Jib_Adegunloye I work with @Isaac_Martin and we are back onto trying to get this to work.

I ran your code inside our docker image running inside the docker-compose, and am getting the same exact error:

> import pymongo
> >>> client = pymongo.MongoClient("mongodb")
> >>> client.admin.command('ping')
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
>   File "/usr/local/lib/python3.11/site-packages/pymongo/_csot.py", line 105, in csot_wrapper
>     return func(self, *args, **kwargs)
>            ^^^^^^^^^^^^^^^^^^^^^^^^^^^
>   File "/usr/local/lib/python3.11/site-packages/pymongo/database.py", line 805, in command
>     with self.__client._socket_for_reads(read_preference, session) as (
>          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>   File "/usr/local/lib/python3.11/site-packages/pymongo/mongo_client.py", line 1296, in _socket_for_reads
>     server = self._select_server(read_preference, session)
>              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>   File "/usr/local/lib/python3.11/site-packages/pymongo/mongo_client.py", line 1257, in _select_server
>     server = topology.select_server(server_selector)
>              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>   File "/usr/local/lib/python3.11/site-packages/pymongo/topology.py", line 272, in select_server
>     server = self._select_server(selector, server_selection_timeout, address)
>              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>   File "/usr/local/lib/python3.11/site-packages/pymongo/topology.py", line 261, in _select_server
>     servers = self.select_servers(selector, server_selection_timeout, address)
>               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>   File "/usr/local/lib/python3.11/site-packages/pymongo/topology.py", line 223, in select_servers
>     server_descriptions = self._select_servers_loop(selector, server_timeout, address)
>                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>   File "/usr/local/lib/python3.11/site-packages/pymongo/topology.py", line 238, in _select_servers_loop
>     raise ServerSelectionTimeoutError(
> pymongo.errors.ServerSelectionTimeoutError: Could not reach any servers in [('localhost', 27017)]. Replica set is configured with internal hostnames or IPs?, Timeout: 30s, Topology Description: <TopologyDescription id: 667c686c97fcf98e6da8891e, topology_type: ReplicaSetNoPrimary, servers: [<ServerDescription ('localhost', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('localhost:27017: [Errno 111] Connection refused')>]>

This is our service config in docker-compose:

  mongodb:
    image: mongodb/mongodb-atlas-local:latest
    container_name: mongodb
    ports:
      - 27017:27017
    volumes:
      - mongodb:/data/db