Connecting wit mongodb service

I have a docker system contains a service that connects with mongodb service. During boot of the service I noticed the message “org.mongodb.driver.cluster: Exception in monitor thread while connecting to server localhost:27017” while the container and host name of the mongodb service is mongo1db.
When the services are up and running the services is trying to connect with “localhost:27017” while the service environment and configuration to use mongo1db:27017 and when I login the service container (docker exec -it service /bin/bash) I can network connect to mongodb:27017 and no other. How I can fix this?

Hey @Muhammed_Alghwell,

In my understanding, it appears that the service inside your Docker container is attempting to establish a connection with MongoDB using “localhost:27017” as the hostname and port. However, you want it to connect to the MongoDB service with the hostname “mongo1db” and port “27017.” This issue is likely the result of some misconfiguration in your YAML file.

I suspect that the correct mapping between the container environment and the MongoDB process running on the localhost may not be in place. If you intend to connect to the localhost, please ensure that the local hostname contains the entry ‘host.docker.internal’ in the ‘/etc/hosts’ file on your system.

Could you kindly verify that your MongoDB connection string uses ‘mongo1db’ as the host, rather than ‘localhost’ or ‘’? Also, try flushing any cached DNS lookups by restarting the Docker daemon and the service containers. The service may have initially resolved localhost.

Perhaps to understand better, it would be helpful if you could share the following information:

  • How is the connection attempted? Is it through a YAML file or a direct connection with the container?
  • Kindly provide the YAML file for closer examination.
  • Also, I would like to know which MongoDB image you are currently using.

Best regards,