Skip to content
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

Closed
milkcoke opened this issue Apr 25, 2023 · 2 comments
Closed

Failed to connect to mongodb #1

milkcoke opened this issue Apr 25, 2023 · 2 comments
Assignees
Labels
bug Something isn't working

Comments

@milkcoke
Copy link
Owner

mongoose.connect(
  'mongodb://max:secret@goals-mongodb:27017/course-goals?authSource=admin',
)

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 }

@milkcoke milkcoke added the bug Something isn't working label Apr 25, 2023
@milkcoke milkcoke self-assigned this Apr 25, 2023
@milkcoke
Copy link
Owner Author

Default docker volume directory path is /var/lib/docker.
image
image

@milkcoke
Copy link
Owner Author

milkcoke commented May 1, 2023

Default bridge network

# run docker container with `default bridge network`
$ docker run -it --name goals-mongodb --rm -v mongo-data:/data/db mongodb
$  docker container ls
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS          PORTS       NAMES
969e474a667e   e09d4dc87e74   "docker-entrypoint.s…"   11 minutes ago   Up 11 minutes   27017/tcp   goals-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?

Differences between user-defined bridges and the default bridge
User-defined bridges provide automatic DNS resolution between containers.
Containers on the default bridge network can only access each other by IP addresses, unless you use the --link option, which is considered legacy. On a user-defined bridge network, containers can resolve each other by name or alias. - Docker docs

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
      }

@milkcoke milkcoke closed this as completed May 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant