Denrox / nestjs-microservices-example

A working example of microservice architecture implementation using nestjs framework
883 stars 257 forks source link

MongoNetworkError: failed to connect to server #5

Open monolithed opened 3 years ago

monolithed commented 3 years ago
➜ cd ./gateway && npm install && npm run test

> gateway@0.0.1 test
> jest --config ./test/jest-e2e.json --detectOpenHandles

(node:32062) DeprecationWarning: 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.
(Use `node --trace-deprecation ...` to show where the warning was created)
 FAIL  test/task.e2e-spec.ts
  ● Tasks (e2e) › /users/ (POST) - should create a user for checking tasks api

    MongoNetworkError: failed to connect to server [db:27017] on first connect [MongoNetworkError: getaddrinfo ENOTFOUND db

      at connectionFailureError (../node_modules/mongodb/lib/core/connection/connect.js:340:14)
      at Socket.<anonymous> (../node_modules/mongodb/lib/core/connection/connect.js:310:16)
        errno: -3008,
        code: 'ENOTFOUND',
        syscall: 'getaddrinfo',
        hostname: 'db'
      }]
      at Pool.<anonymous> (../node_modules/mongodb/lib/core/topologies/server.js:438:11)
      at ../node_modules/mongodb/lib/core/connection/pool.js:562:14
      at ../node_modules/mongodb/lib/core/connection/pool.js:995:11
      at ../node_modules/mongodb/lib/core/connection/connect.js:32:7
      at callback (../node_modules/mongodb/lib/core/connection/connect.js:280:5)
      at Socket.<anonymous> (../node_modules/mongodb/lib/core/connection/connect.js:310:7)
➜ ls ./node_modules/mongodb/lib/core/connection/connect.js
./node_modules/mongodb/lib/core/connection/connect.js
➜ docker images
REPOSITORY                                TAG       IMAGE ID       CREATED       SIZE
nestjs-microservices-example_task         latest    8f6ea10c6c9c   2 hours ago   301MB
nestjs-microservices-example_token        latest    d91cd08050e6   2 hours ago   304MB
nestjs-microservices-example_user         latest    4ea9ae968a07   2 hours ago   305MB
nestjs-microservices-example_permission   latest    45fc5c5b4651   2 hours ago   287MB
nestjs-microservices-example_mailer       latest    bc43a7509bca   2 hours ago   312MB
nestjs-microservices-example_gateway      latest    be198aa8b10e   2 hours ago   376MB
mongo                                     3.7       3db1239d511b   3 years ago   365MB
➜ docker network ls
NETWORK ID     NAME                                   DRIVER    SCOPE
c1f91c7540aa   bridge                                 bridge    local
cd35c2d8606f   host                                   host      local
e202db18ab24   infrastructure                         bridge    local
bb8f103e8d56   nestjs-microservices-example_backend   bridge    local
ae658c26cd1e   none                                   null      local
➜ ls -l
total 1224
-rw-r--r--    1 monolithed  staff     188 26 июл 10:06 Dockerfile
-rw-r--r--    1 monolithed  staff      84 26 июл 10:06 nest-cli.json
drwxr-xr-x  524 monolithed  staff   16768 26 июл 11:35 node_modules
-rw-r--r--    1 monolithed  staff  598555 26 июл 11:35 package-lock.json
-rw-r--r--    1 monolithed  staff    1555 26 июл 11:35 package.json
drwxr-xr-x    9 monolithed  staff     288 26 июл 10:06 src
drwxr-xr-x   11 monolithed  staff     352 26 июл 10:06 test
-rw-r--r--    1 monolithed  staff      97 26 июл 10:06 tsconfig.build.json
-rw-r--r--    1 monolithed  staff     362 26 июл 10:06 tsconfig.json
-rw-r--r--    1 monolithed  staff     503 26 июл 10:06 tslint.json
monolithed commented 3 years ago

There was a typo in the .env file, however that bug fix did not solve the issue

-MAILER_FROM="Test mail" <someusername@denrox.com>
+MAILER_FROM="Test mail <someusername@denrox.com>"
Denrox commented 3 years ago

Hi, thank you for the report! Please confirm that before running tests command you executed steps:

If yes, then could you please leave a comment here with the results of command "docker ps -a" You should have a mongo container up and running with hostname "db" and the port number "27017"

monolithed commented 3 years ago
➜ docker ps
CONTAINER ID   IMAGE                                     COMMAND                  CREATED      STATUS              PORTS                                           NAMES
ba89956a7c37   nestjs-microservices-example_task         "docker-entrypoint.s…"   7 days ago   Up About a minute   0.0.0.0:8001->8001/tcp, :::8001->8001/tcp       nestjs-microservices-example_task_1
e60a36b6ced3   nestjs-microservices-example_user         "docker-entrypoint.s…"   7 days ago   Up About a minute   0.0.0.0:8003->8003/tcp, :::8003->8003/tcp       nestjs-microservices-example_user_1
6af4fe7b8847   nestjs-microservices-example_token        "docker-entrypoint.s…"   7 days ago   Up About a minute   0.0.0.0:8002->8002/tcp, :::8002->8002/tcp       nestjs-microservices-example_token_1
dceed300bb9a   mongo:3.7                                 "docker-entrypoint.s…"   7 days ago   Up About a minute   0.0.0.0:27017->27017/tcp, :::27017->27017/tcp   nestjs-microservices-example_db_1
2bdf18076dd4   nestjs-microservices-example_mailer       "docker-entrypoint.s…"   7 days ago   Up About a minute   0.0.0.0:8004->8004/tcp, :::8004->8004/tcp       nestjs-microservices-example_mailer_1
e8c6cb753717   nestjs-microservices-example_permission   "docker-entrypoint.s…"   7 days ago   Up About a minute   0.0.0.0:8005->8005/tcp, :::8005->8005/tcp       nestjs-microservices-example_permission_1
5d75b795a4ed   nestjs-microservices-example_gateway      "docker-entrypoint.s…"   7 days ago   Up About a minute   0.0.0.0:8000->8000/tcp, :::8000->8000/tcp       nestjs-microservices-example_gateway_1
➜ curl -X 'GET' \
>   'http://localhost:8000/users' \
>   -H 'accept: application/json'
{"message":"token_decode_unauthorized","data":null,"errors":null}

It looks like the user was not created

Denrox commented 3 years ago

Hi, I suggest you to apply a temporary fix for this issue

Please add a record 127.0.0.1 db To the /etc/hosts file on your machine

To get rid of previously created containers with errors, pls run docker rm -f $(docker ps -aq | grep "nestjs")

After this you can rerun steps for running tests.

This is kind of a temporary spike. But I will introduce a proper fix via docker networking on next update

chauchausoup commented 1 year ago

Hi, I suggest you to apply a temporary fix for this issue

Please add a record 127.0.0.1 db To the /etc/hosts file on your machine

To get rid of previously created containers with errors, pls run docker rm -f $(docker ps -aq | grep "nestjs")

After this you can rerun steps for running tests.

This is kind of a temporary spike. But I will introduce a proper fix via docker networking on next update

Waiting for the update.