skip to Main Content

I’m having an issue with the connection between my MongoDB that I launched with docker (port:27017) but when I try to launch my app (the DB is needed for launching it), I am stuck with this error : MongoServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017.

I tried the solution at Connecting to Mongodb docker container from another docker container but sill could not figure how to solve this issue…

Anyone have a thought about this issue ?

docker run chat-ui       

                        -------------

__/\\\\\\____/\\____________/\\____/\\\\_____
 _/\/////////\_/\\\________/\\\__/\///////\___
  _/\_______/\_/\//\____/\//\_///______//\__
   _/\\\\\\/__/\\///\/\/_/\___________/\/___
    _/\/////////____/\__///\/___/\________/\//_____
     _/\_____________/\____///_____/\_____/\//________
      _/\_____________/\_____________/\___/\/___________
       _/\_____________/\_____________/\__/\\\\\\\_
        _///______________///______________///__///////////////__


                          Runtime Edition

        PM2 is a Production Process Manager for Node.js applications
                     with a built-in Load Balancer.

                Start and Daemonize any application:
                $ pm2 start app.js

                Load Balance 4 instances of api.js:
                $ pm2 start api.js -i 4

                Monitor in production:
                $ pm2 monitor

                Make pm2 auto-boot at server restart:
                $ pm2 startup

                To go further checkout:
                http://pm2.io/


                        -------------

pm2 launched in no-daemon mode (you can add DEBUG="*" env variable to get more messages)
2023-06-19T09:18:59: PM2 log: Launching in no daemon mode
2023-06-19T09:18:59: PM2 log: [PM2] Starting /app/build/index.js in cluster_mode (0 instance)
2023-06-19T09:18:59: PM2 log: App [index:0] starting in -cluster mode-
2023-06-19T09:18:59: PM2 log: App [index:0] online
2023-06-19T09:18:59: PM2 log: App [index:1] starting in -cluster mode-
2023-06-19T09:18:59: PM2 log: App [index:1] online
2023-06-19T09:18:59: PM2 log: App [index:2] starting in -cluster mode-
2023-06-19T09:18:59: PM2 log: App [index:2] online
2023-06-19T09:18:59: PM2 log: App [index:3] starting in -cluster mode-
2023-06-19T09:18:59: PM2 log: App [index:3] online
2023-06-19T09:18:59: PM2 log: [PM2] Done.
2023-06-19T09:18:59: PM2 log: ┌────┬──────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬──────────┬──────────┬──────────┬──────────┐
│ id │ name     │ namespace   │ version │ mode    │ pid      │ uptime │ ↺    │ status    │ cpu      │ mem      │ user     │ watching │
├────┼──────────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┼──────────┼──────────┼──────────┼──────────┤
│ 0  │ index    │ default     │ 0.3.0   │ cluster │ 19       │ 0s     │ 0    │ online    │ 0%       │ 61.7mb   │ root     │ disabled │
│ 1  │ index    │ default     │ 0.3.0   │ cluster │ 26       │ 0s     │ 0    │ online    │ 0%       │ 52.9mb   │ root     │ disabled │
│ 2  │ index    │ default     │ 0.3.0   │ cluster │ 33       │ 0s     │ 0    │ online    │ 0%       │ 51.0mb   │ root     │ disabled │
│ 3  │ index    │ default     │ 0.3.0   │ cluster │ 44       │ 0s     │ 0    │ online    │ 0%       │ 45.3mb   │ root     │ disabled │
└────┴──────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘
2023-06-19T09:18:59: PM2 log: [--no-daemon] Continue to stream logs
2023-06-19T09:18:59: PM2 log: [--no-daemon] Exit on target PM2 exit pid=8
09:18:59 0|index  | Listening on 0.0.0.0:3000
09:18:59 1|index  | Listening on 0.0.0.0:3000
09:18:59 2|index  | Listening on 0.0.0.0:3000
09:18:59 3|index  | Listening on 0.0.0.0:3000
09:19:29 0|index  | MongoServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017
09:19:29 0|index  |     at Timeout._onTimeout (/app/node_modules/mongodb/lib/sdam/topology.js:277:38)
09:19:29 0|index  |     at listOnTimeout (node:internal/timers:573:17)
09:19:29 0|index  |     at process.processTimers (node:internal/timers:514:7) {
09:19:29 0|index  |   reason: TopologyDescription {
09:19:29 0|index  |     type: 'Unknown',
09:19:29 0|index  |     servers: Map(1) { 'localhost:27017' => [ServerDescription] },
09:19:29 0|index  |     stale: false,
09:19:29 0|index  |     compatible: true,
09:19:29 0|index  |     heartbeatFrequencyMS: 10000,
09:19:29 0|index  |     localThresholdMS: 15,
09:19:29 0|index  |     setName: null,
09:19:29 0|index  |     maxElectionId: null,
09:19:29 0|index  |     maxSetVersion: null,
09:19:29 0|index  |     commonWireVersion: 0,
09:19:29 0|index  |     logicalSessionTimeoutMinutes: null
09:19:29 0|index  |   },
09:19:29 0|index  |   code: undefined,
09:19:29 0|index  |   [Symbol(errorLabels)]: Set(0) {}
09:19:29 0|index  | }
09:19:29 1|index  | MongoServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017
09:19:29 1|index  |     at Timeout._onTimeout (/app/node_modules/mongodb/lib/sdam/topology.js:277:38)
09:19:29 1|index  |     at listOnTimeout (node:internal/timers:573:17)
09:19:29 1|index  |     at process.processTimers (node:internal/timers:514:7) {
09:19:29 1|index  |   reason: TopologyDescription {
09:19:29 1|index  |     type: 'Unknown',
09:19:29 1|index  |     servers: Map(1) { 'localhost:27017' => [ServerDescription] },
09:19:29 1|index  |     stale: false,
09:19:29 1|index  |     compatible: true,
09:19:29 1|index  |     heartbeatFrequencyMS: 10000,
09:19:29 1|index  |     localThresholdMS: 15,
09:19:29 1|index  |     setName: null,
09:19:29 1|index  |     maxElectionId: null,
09:19:29 1|index  |     maxSetVersion: null,
09:19:29 1|index  |     commonWireVersion: 0,
09:19:29 1|index  |     logicalSessionTimeoutMinutes: null
09:19:29 1|index  |   },
09:19:29 1|index  |   code: undefined,
09:19:29 1|index  |   [Symbol(errorLabels)]: Set(0) {}
09:19:29 1|index  | }
09:19:29 2|index  | MongoServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017
09:19:29 2|index  |     at Timeout._onTimeout (/app/node_modules/mongodb/lib/sdam/topology.js:277:38)
09:19:29 2|index  |     at listOnTimeout (node:internal/timers:573:17)
09:19:29 2|index  |     at process.processTimers (node:internal/timers:514:7) {
09:19:29 2|index  |   reason: TopologyDescription {
09:19:29 2|index  |     type: 'Unknown',
09:19:29 2|index  |     servers: Map(1) { 'localhost:27017' => [ServerDescription] },
09:19:29 2|index  |     stale: false,
09:19:29 2|index  |     compatible: true,
09:19:29 2|index  |     heartbeatFrequencyMS: 10000,
09:19:29 2|index  |     localThresholdMS: 15,
09:19:29 2|index  |     setName: null,
09:19:29 2|index  |     maxElectionId: null,
09:19:29 2|index  |     maxSetVersion: null,
09:19:29 2|index  |     commonWireVersion: 0,
09:19:29 2|index  |     logicalSessionTimeoutMinutes: null
09:19:29 2|index  |   },
09:19:29 2|index  |   code: undefined,
09:19:29 2|index  |   [Symbol(errorLabels)]: Set(0) {}
09:19:29 2|index  | }
09:19:29 3|index  | MongoServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017
09:19:29 3|index  |     at Timeout._onTimeout (/app/node_modules/mongodb/lib/sdam/topology.js:277:38)
09:19:29 3|index  |     at listOnTimeout (node:internal/timers:573:17)
09:19:29 3|index  |     at process.processTimers (node:internal/timers:514:7) {
09:19:29 3|index  |   reason: TopologyDescription {
09:19:29 3|index  |     type: 'Unknown',
09:19:29 3|index  |     servers: Map(1) { 'localhost:27017' => [ServerDescription] },
09:19:29 3|index  |     stale: false,
09:19:29 3|index  |     compatible: true,
09:19:29 3|index  |     heartbeatFrequencyMS: 10000,
09:19:29 3|index  |     localThresholdMS: 15,
09:19:29 3|index  |     setName: null,
09:19:29 3|index  |     maxElectionId: null,
09:19:29 3|index  |     maxSetVersion: null,
09:19:29 3|index  |     commonWireVersion: 0,
09:19:29 3|index  |     logicalSessionTimeoutMinutes: null
09:19:29 3|index  |   },
09:19:29 3|index  |   code: undefined,
09:19:29 3|index  |   [Symbol(errorLabels)]: Set(0) {}
09:19:29 3|index  | }

I tried using an online DB but I was still getting an error (MongoServerError: bad auth : authentication failed) because I wanted it to launch on my server, I didn’t wanted to spend too much time on this solution (even in the case it would be working, which was not the case).

I tried using mongodb://myip:27017 instead of mongodb://localhost:27017 yet, still having an issue.

2

Answers


  1. Have you exposed the mongo container’s 27017 port to your host?

    Try running the mongo container with either:

    docker run -p 27017:27017 --name some-mongo -d mongo:tag
    

    or:

    docker run --network="host" --name some-mongo -d mongo:tag
    
    Login or Signup to reply.
  2. When you use localhost in your container, It doesn’t mean your PC, in the context of a container localhost means the container itself. The services inside a container runs in an isolated environment.
    So that’s why the container chat-ui unable to connect with the mongodb container with localhost:27017.

    The way to connect different containers

    • You can use Docker Networking. As this allows containers to communicate with each other in a network.
    • OR you can deploy multiple containers by using Docker compose
    • There is another approach for container linking with Legacy container links. But this is not recommended as it may eventually be removed

    Also see these

    Hope this will help you to resolve

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search