New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Failed to connect to mongodb #1
Comments
Default bridge network# run docker container with `default bridge network`
$ docker run -it --name goals-mongodb --rm -v mongo-data:/data/db mongodb
I tried to nodejs backend application using container name (async ()=>{
try {
await mongoose.connect(
'mongodb://max:secret@goals-mongodb:27017/course-goals?authSource=admin',
{
useNewUrlParser: true,
useUnifiedTopology: true,
},
);
console.log('Success to connect mongoDB')
} catch (e) {
// error occurs: ENOTFOUND `goals-mongodb`
console.error(e)
}
})() Why do error occur?
How to resolve this?Use user-defined bridges which provides automatic DNS resolution between containers. mongodb container$ docker run -it --name goals-mongodb --rm -v mongo-data:/data/db \
# user defined network
--network goals-network mongodb nodejs container$ docker run -it --name goals-backend-node --rm -v node_moduels \
--network goals-network nodejs-app Success to connect each other. $ docker container inspect goals-mongodb mongodb inspect network "Networks" : {
"goals-network" : {
"IPAMConfig" : null,
"Links" : null,
"Aliases" : [ "4f33e3879e54" ],
"NetworkID" : "30986f5672a94f72f02e7088996e0c3004233173ef6b0614865287a50a9c35e6",
"EndpointID" : "376017da69d5ef9750a9f532b5dfe70a729bdfe2b3aa41e0736794a433816ab6",
"Gateway" : "172.19.0.1",
"IPAddress" : "172.19.0.2",
"IPPrefixLen" : 16,
"IPv6Gateway" : "",
"GlobalIPv6Address" : "",
"GlobalIPv6PrefixLen" : 0,
"MacAddress" : "02:42:ac:13:00:02",
"DriverOpts" : null
} nodejs inspect network$ docker container inspect goals-backend-node "Networks" : {
"goals-network" : {
"IPAMConfig" : null,
"Links" : null,
"Aliases" : [ "beec4b155658" ],
"NetworkID" : "30986f5672a94f72f02e7088996e0c3004233173ef6b0614865287a50a9c35e6",
"EndpointID" : "d0f7721f44f3ca710a3bb8a4379e3bec34632de31aa0bbe965db4236cdd97a97",
"Gateway" : "172.19.0.1",
"IPAddress" : "172.19.0.3",
"IPPrefixLen" : 16,
"IPv6Gateway" : "",
"GlobalIPv6Address" : "",
"GlobalIPv6PrefixLen" : 0,
"MacAddress" : "02:42:ac:13:00:03",
"DriverOpts" : null
} |
Already set mongodb -e
FAILED TO CONNECT TO MONGODB
2023-04-25T22:59:36.462919176Z MongoError: Authentication failed.
2023-04-25T22:59:36.462946325Z at MessageStream.messageHandler (/app/node_modules/mongodb/lib/cmap/connection.js:299:20)
2023-04-25T22:59:36.462950245Z at MessageStream.emit (node:events:511:28)
2023-04-25T22:59:36.462952535Z at processIncomingData (/app/node_modules/mongodb/lib/cmap/message_stream.js:144:12)
2023-04-25T22:59:36.462954835Z at MessageStream._write (/app/node_modules/mongodb/lib/cmap/message_stream.js:42:5)
2023-04-25T22:59:36.462957235Z at writeOrBuffer (node:internal/streams/writable:399:12)
2023-04-25T22:59:36.462959355Z at _write (node:internal/streams/writable:340:10)
2023-04-25T22:59:36.462961415Z at Writable.write (node:internal/streams/writable:344:10)
2023-04-25T22:59:36.462963505Z at Socket.ondata (node:internal/streams/readable:774:22)
2023-04-25T22:59:36.462965535Z at Socket.emit (node:events:511:28)
2023-04-25T22:59:36.462967545Z at addChunk (node:internal/streams/readable:332:12)
2023-04-25T22:59:36.462969555Z at readableAddChunk (node:internal/streams/readable:305:9)
2023-04-25T22:59:36.462971595Z at Readable.push (node:internal/streams/readable:242:10)
2023-04-25T22:59:36.462978035Z at TCP.onStreamRead (node:internal/stream_base_commons:190:23) {
2023-04-25T22:59:36.462980295Z ok: 0,
2023-04-25T22:59:36.462982335Z code: 18,
2023-04-25T22:59:36.462984375Z codeName: 'AuthenticationFailed'
2023-04-25T22:59:36.462986405Z }
The text was updated successfully, but these errors were encountered: