Docker containers: MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017

Hello,

So I currently stuck on this issue for the past couple of hours. I have a linux server with my MongoDB database running inside of a docker container - 0.0.0.0:27017->27017/tcp. I am able to connect it from the outside of the vps itself. But the issue is that I am running another docker container trying to connect to the MongoDB server on the same vps and it results in this error.

For the mongo uri string I tried the following
mongodb://username:password@127.0.0.1:27017
mongodb://username:password@0.0.0.0:27017
mongodb://username:password@localhost:27017
mongodb://username:password@ipaddress:27017

For the ufw rules itself, I added the vps’s IP addresses, 127.0.0.1 to allow connection to port 27017, but no matter what I keep running into the same issue.

 Error connecting to MongoDB: MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017
    at _handleConnectionErrors (/app/node_modules/mongoose/lib/connection.js:1165:11)
    at NativeConnection.openUri (/app/node_modules/mongoose/lib/connection.js:1096:11) {
  errorLabelSet: Set(0) {},
  reason: TopologyDescription {
    type: 'Unknown',
    servers: Map(1) { '127.0.0.1:27017' => [ServerDescription] },
    stale: false,
    compatible: true,
    heartbeatFrequencyMS: 10000,
    localThresholdMS: 15,
    setName: null,
    maxElectionId: null,
    maxSetVersion: null,
    commonWireVersion: 0,
    logicalSessionTimeoutMinutes: null
  },
  code: undefined
}