I followed official mongo image description trying to using it with docker-compose.But i’ve noticed that the mongo service name can not be the same as the image name.Here is the situation:
created a docker-compose.yml file which filled with this:
Could not connect to database using connectionString: mongodb://root:example@mongo:27017/"
mongo-express_1 | (node:8) UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [mongo:27017] on first connect [MongoError: Authentication failed.
mongo-express_1 | at Connection.messageHandler (/node_modules/mongodb/lib/core/connection/connection.js:364:19)
mongo-express_1 | at Connection.emit (events.js:314:20)
But when I change the service name mongo to myMongo,it just work out!:
Hello i also have the same problem (even when i change the mongo service name…)
(node:7) [MONGODB DRIVER] Warning: Current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
Could not connect to database using connectionString: mongodb://root:example@mongo:27017/"
(node:7) UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [mongo:27017] on first connect [Error: connect ECONNREFUSED 192.168.160.2:27017
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1144:16) {
name: 'MongoNetworkError'
}]
I’m using the exact docker-compose example in the documentation : Docker
The issue you’re encountering is related to the hostname of the MongoDB service that you’re trying to connect to from your mongo-express service.
In the first example where you named the service mongo, the hostname mongo is already being used by the mongo image as its own hostname. This means that the mongo-express service cannot connect to the MongoDB service as it cannot resolve the hostname mongo to the correct IP address.
In the second example where you renamed the mongo service to myMongo, you’re using a different hostname that is not being used by the mongo image, so the mongo-express service can correctly resolve the hostname to the MongoDB service.
To avoid this issue, you can either rename your MongoDB service to something other than mongo or choose a different hostname to connect to the service.
Alternatively, you could define a custom network for your Docker Compose setup and ensure that both the mongo and mongo-express services are on the same network, which would allow them to communicate with each other using their service names as hostnames.