Learn how to resolve the common issue of `MongooseServerSelectionError` when connecting to MongoDB in Docker-Compose containers. --- This video is based on the question https://stackoverflow.com/q/65597319/ asked by the user 'Erez Shlomo' ( https://stackoverflow.com/u/9467167/ ) and on the answer https://stackoverflow.com/a/65610502/ provided by the user 'Erez Shlomo' ( https://stackoverflow.com/u/9467167/ ) at 'Stack Overflow' website. Thanks to these great users and Stackexchange community for their contributions. Visit these links for original content and any more details, such as alternate solutions, latest updates/developments on topic, comments, revision history etc. For example, the original title of the Question was: Connecting to MongoDB with Mongoose in docker-compose containers fail with useUnifiedTopology Also, Content (except music) licensed under CC BY-SA https://meta.stackexchange.com/help/l... The original Question post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license, and the original Answer post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license. If anything seems off to you, please feel free to write me at vlogize [AT] gmail [DOT] com. --- Troubleshooting Connection Issues: Mongoose and MongoDB in Docker-Compose Setting up a Node.js application to connect with MongoDB can sometimes lead to frustrating issues, particularly when using Docker for containers. One common error that developers encounter is the MongooseServerSelectionError when using the useUnifiedTopology option. In this blog, we'll dive into what this error means and how you can resolve it effectively. The Problem Imagine you have a Node.js application deploying both your app and MongoDB in Docker containers using docker-compose. Your intention is to follow best practices and use the useUnifiedTopology feature in Mongoose to improve the connection management. Upon trying to connect, you receive the error message: [[See Video to Reveal this Text or Code Snippet]] This error can be confusing, especially since commenting out the useUnifiedTopology option allows the connection to succeed. Let's break down what’s happening and how to fix it. Understanding the Error The ECONNREFUSED message indicates that your application tried to connect to MongoDB at 127.0.0.1:27017 but was refused. This discrepancy is usually due to how the container networking is set up. Why It Occurs: Incorrect Host Configuration: When specifying the connection string, your Node.js application attempts to connect to MongoDB using the localhost address (127.0.0.1), which refers to the Node.js container itself rather than the MongoDB container. Networking Between Containers: In Docker, each service/container gets its own network namespace and cannot access services using localhost or 127.0.0.1. Instead, you should use the container name defined in your docker-compose.yml file as the hostname in your connection string. The Solution Update Your Connection String To fix the connection issue, you need to correct your MongoDB connection string: Modify your connection string to point to the MongoDB container directly using the container name instead of localhost: [[See Video to Reveal this Text or Code Snippet]] Notice the change from 127.0.0.1 to mongo, which is the service name defined in your docker-compose.yml. Check Your "hosts" File Configuration Sometimes, the error stems from incorrect entries in your system's "hosts" file. Ensure that there are no discrepancies or erroneous mappings that could direct your application incorrectly. Ensure Docker Services Are Up Make sure all your services are up and running properly. You can do this using the following command: [[See Video to Reveal this Text or Code Snippet]] This command will build and start your containers as defined in the docker-compose.yml file. Example of Corrected Code Here’s how your connection setup should look in your Node.js application after these changes: [[See Video to Reveal this Text or Code Snippet]] Conclusion Navigating through containerized environments like Docker can sometimes present unexpected challenges, particularly with configurations like connecting to databases. By ensuring you use the correct hostnames in your connection strings and double-checking your "hosts" file configuration, you should be able to establish a successful connection with MongoDB using Mongoose in no time. If you continue to encounter issues, consider revisiting your Docker and Mongoose configurations or seeking further community help. Happy coding!