MongoDB shows timeout error when connected with flask over docker

I’ve been trying to connect flask with mongodb over docker but constantly get the timeout error. Here’s my code and error below. Please let me know where I’ve gone wrong? Thanks.

Also, I’ve intentionally chosen port 27018 instead of 27017

app.py code:

from pymongo import MongoClient

client = MongoClient(host="test_mongodb",
                     port = 27018,
                     username = "root",
                     password = "rootpassword",
                     authSource = "admin"
                    )

#db is same as directory created to identify database
#default port is 27017

db = client.aNewDB
#db is a new database 
UserNum = db["UserNum"]
#UserNum is a new Collection
UserNum.insert_one({'num_of_users':0})

docker-compose.yml

version: '3'

services:
  web:
    build: ./Web
    ports: 
      - "5000:5000"
    links:
      - db #Web is dependent on db
  db:
    image: mongo:latest
    hostname: test_mongodb
    environment:
      - MONGO_INITDB_ROOT_USERNAME=admin
      - MONGO_INITDB_ROOT_PASSWORD=password
    ports:
      - 27018:27018

Error during docker-compose up:

web_1  | Traceback (most recent call last):
web_1  |   File "app.py", line 21, in <module>
web_1  |     UserNum.insert_one({'num_of_users':0})
web_1  |   File "/usr/local/lib/python3.7/site-packages/pymongo/collection.py", line 628, in insert_one
web_1  |     comment=comment,
web_1  |   File "/usr/local/lib/python3.7/site-packages/pymongo/collection.py", line 562, in _insert_one
web_1  |     self.__database.client._retryable_write(acknowledged, _insert_command, session)
web_1  |   File "/usr/local/lib/python3.7/site-packages/pymongo/mongo_client.py", line 1447, in _retryable_write
web_1  |     with self._tmp_session(session) as s:
web_1  |   File "/usr/local/lib/python3.7/contextlib.py", line 112, in __enter__
web_1  |     return next(self.gen)
web_1  |   File "/usr/local/lib/python3.7/site-packages/pymongo/mongo_client.py", line 1729, in _tmp_session
web_1  |     s = self._ensure_session(session)
web_1  |   File "/usr/local/lib/python3.7/site-packages/pymongo/mongo_client.py", line 1712, in _ensure_session
web_1  |     return self.__start_session(True, causal_consistency=False)
web_1  |   File "/usr/local/lib/python3.7/site-packages/pymongo/mongo_client.py", line 1657, in __start_session
web_1  |     self._topology._check_implicit_session_support()
web_1  |   File "/usr/local/lib/python3.7/site-packages/pymongo/topology.py", line 538, in _check_implicit_session_support
web_1  |     self._check_session_support()
web_1  |   File "/usr/local/lib/python3.7/site-packages/pymongo/topology.py", line 555, in _check_session_support
web_1  |     readable_server_selector, self.get_server_selection_timeout(), None
web_1  |   File "/usr/local/lib/python3.7/site-packages/pymongo/topology.py", line 240, in _select_servers_loop
web_1  |     % (self._error_message(selector), timeout, self.description)
web_1  | pymongo.errors.ServerSelectionTimeoutError: test_mongodb:27018: timed out, Timeout: 30s, Topology Description: <TopologyDescription id: 62fa0685c58c2b61f79ea52e, topology_type: Unknown, servers: [<ServerDescription ('test_mongodb', 27018) server_type: Unknown, rtt: None, error=NetworkTimeout('test_mongodb:27018: timed out')>]>
flask_project_web_1 exited with code 1

Try with the default port 27017 and share the result.

Hi @steevej I tried running the same with port 27017.

I have mongodb running locally on that port so I got the following message:

ERROR: for flask_project_db_1  Cannot start service db: driver failed programming external connectivity on endpoint flask_project_db_1 (41241e60d62271a299edab7d1ef56cb9d5fa3328cb9028c13f093972d796b915): Error starting userland proxy: listen tcp4 0.0.0.0:27017: bind: address already in use

ERROR: for db  Cannot start service db: driver failed programming external connectivity on endpoint flask_project_db_1 (41241e60d62271a299edab7d1ef56cb9d5fa3328cb9028c13f093972d796b915): Error starting userland proxy: listen tcp4 0.0.0.0:27017: bind: address already in use
ERROR: Encountered errors while bringing up the project.

I performed db.shutdownServer() within mongo to close the same and retried to obtain the response as below:

web_1  | Traceback (most recent call last):
web_1  |   File "app.py", line 25, in <module>
web_1  |     UserNum.insert_one({'num_of_users':0})
web_1  |   File "/usr/local/lib/python3.7/site-packages/pymongo/collection.py", line 628, in insert_one
web_1  |     comment=comment,
web_1  |   File "/usr/local/lib/python3.7/site-packages/pymongo/collection.py", line 562, in _insert_one
web_1  |     self.__database.client._retryable_write(acknowledged, _insert_command, session)
web_1  |   File "/usr/local/lib/python3.7/site-packages/pymongo/mongo_client.py", line 1447, in _retryable_write
web_1  |     with self._tmp_session(session) as s:
web_1  |   File "/usr/local/lib/python3.7/contextlib.py", line 112, in __enter__
web_1  |     return next(self.gen)
web_1  |   File "/usr/local/lib/python3.7/site-packages/pymongo/mongo_client.py", line 1729, in _tmp_session
web_1  |     s = self._ensure_session(session)
web_1  |   File "/usr/local/lib/python3.7/site-packages/pymongo/mongo_client.py", line 1712, in _ensure_session
web_1  |     return self.__start_session(True, causal_consistency=False)
web_1  |   File "/usr/local/lib/python3.7/site-packages/pymongo/mongo_client.py", line 1657, in __start_session
web_1  |     self._topology._check_implicit_session_support()
web_1  |   File "/usr/local/lib/python3.7/site-packages/pymongo/topology.py", line 538, in _check_implicit_session_support
web_1  |     self._check_session_support()
web_1  |   File "/usr/local/lib/python3.7/site-packages/pymongo/topology.py", line 555, in _check_session_support
web_1  |     readable_server_selector, self.get_server_selection_timeout(), None
web_1  |   File "/usr/local/lib/python3.7/site-packages/pymongo/topology.py", line 240, in _select_servers_loop
web_1  |     % (self._error_message(selector), timeout, self.description)
web_1  | pymongo.errors.ServerSelectionTimeoutError: test_mongodb:27017: timed out, Timeout: 30s, Topology Description: <TopologyDescription id: 62fa397096b8563268d159b9, topology_type: Unknown, servers: [<ServerDescription ('test_mongodb', 27017) server_type: Unknown, rtt: None, error=NetworkTimeout('test_mongodb:27017: timed out')>]>

Could you try to split your setup in 2 parts? One for the web part and one for the db part.

It would be easier to find what is the issue.

Make sure you start the db part first, then try to connect manually with mongosh. Once you know that part is working, try the web part.

If you really want to have both in the same you could try to put them back together once you know they both work separately.