I am trying to connect to mongo container from a Python-flask container. I am using below configs,
flask: build: ./application container_name: app restart: unless-stopped depends_on: - dbservice ports: - 5000:5000 nginx: build: ./nginx container_name: nginx restart: unless-stopped ports: - "8080:443" dbservice: image: mongo container_name: dbservice restart: unless-stopped ports: - 27017:27017 volumes: - "/home/admin/Desktop/app/db:/data/db"
And for connecting from mongo client,
When I run the whole thing together, the connection is really inconsistent. i.e: sometimes the flask app connects to the mongo container and sometimes it doesn’t and gives me below errors.
pymongo.errors.ServerSelectionTimeoutError: No servers found yet, Timeout: 30s, Topology Description: <TopologyDescription id: 5fbe9fa507475fa5e28d354c, topology_type: Single, servers: [<ServerDescription (‘dbservice’, 27017) server_type: Unknown, rtt: None>]>
Connection Refused (in the same error format as above)
when the error occurs, the flask app cannot get any db data and it hangs. When the same query made again right after the error ,(i.e: html page of flask app is refreshed) db data is retrieved normally and works normally. (I do not use JS (/frontend) to make any db call either). This behavior changes randomly.
If I use the mongo db and flask without containers, they work as expected without errors. What could be the issue here?