Home > OS >  Mongo ECONNREFUSED Error: Using docker-compose to spin up a container with a backend app and a conta
Mongo ECONNREFUSED Error: Using docker-compose to spin up a container with a backend app and a conta

Time:01-23

I have a custom made docker image for the backend of my app. I have a yaml file that runs my app image and a mongo image. However, when I use docker-compose on the yml file, I get the following error (about 20 seconds and the containers start running):

(node:33) [MONGOOSE] DeprecationWarning: Mongoose: the `strictQuery` option will be switched back to `false` by default in Mongoose 7. Use `mongoose.set('strictQuery', false);` if you want to prepare for this change. Or use `mongoose.set('strictQuery', true);` to suppress this warning.
(Use `node --trace-deprecation ...` to show where the warning was created)
Server listening on port 3000
/cloudband/node_modules/mongoose/lib/connection.js:825
  const serverSelectionError = new ServerSelectionError();
                               ^

MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017
    at Connection.openUri (/cloudband/node_modules/mongoose/lib/connection.js:825:32)
    at /cloudband/node_modules/mongoose/lib/index.js:409:10
    at /cloudband/node_modules/mongoose/lib/helpers/promiseOrCallback.js:41:5
    at new Promise (<anonymous>)
    at promiseOrCallback (/cloudband/node_modules/mongoose/lib/helpers/promiseOrCallback.js:40:10)
    at Mongoose._promiseOrCallback (/cloudband/node_modules/mongoose/lib/index.js:1262:10)
    at Mongoose.connect (/cloudband/node_modules/mongoose/lib/index.js:408:20)
    at Object.<anonymous> (/cloudband/server/server.js:15:4)
    at Module._compile (node:internal/modules/cjs/loader:1239:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1293:10) {
  reason: TopologyDescription {
    type: 'Unknown',
    servers: Map(1) {
      'localhost:27017' => ServerDescription {
        address: 'localhost:27017',
        type: 'Unknown',
        hosts: [],
        passives: [],
        arbiters: [],
        tags: {},
        minWireVersion: 0,
        maxWireVersion: 0,
        roundTripTime: -1,
        lastUpdateTime: 28094812,
        lastWriteDate: 0,
        error: MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017
            at connectionFailureError (/cloudband/node_modules/mongodb/lib/cmap/connect.js:387:20)
            at Socket.<anonymous> (/cloudband/node_modules/mongodb/lib/cmap/connect.js:310:22)
            at Object.onceWrapper (node:events:628:26)
            at Socket.emit (node:events:513:28)
            at emitErrorNT (node:internal/streams/destroy:151:8)
            at emitErrorCloseNT (node:internal/streams/destroy:116:3)
            at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
          cause: Error: connect ECONNREFUSED 127.0.0.1:27017
              at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1495:16) {
            errno: -111,
            code: 'ECONNREFUSED',
            syscall: 'connect',
            address: '127.0.0.1',
            port: 27017
          },
          [Symbol(errorLabels)]: Set(1) { 'ResetPool' }
        },
        topologyVersion: null,
        setName: null,
        setVersion: null,
        electionId: null,
        logicalSessionTimeoutMinutes: null,
        primary: null,
        me: null,
        '$clusterTime': null
      }
    },
    stale: false,
    compatible: true,
    heartbeatFrequencyMS: 10000,
    localThresholdMS: 15,
    setName: null,
    maxElectionId: null,
    maxSetVersion: null,
    commonWireVersion: 0,
    logicalSessionTimeoutMinutes: null
  },
  code: undefined
}

Here are my files: Dockerfile:

FROM node:19.4.0

WORKDIR /cloudband

COPY package.json /cloudband/
COPY package-lock.json /cloudband/

RUN npm ci 

COPY .env /cloudband/
COPY server /cloudband/server/

EXPOSE 3000 

CMD ["npm", "run", "dev:server"]

YAML file:

version: '3'
services:
  mongo:
    image: mongo
    container_name: mongo
    ports:
      - 27017:27017
    environment:
      - MONGO_INITDB_ROOT_USERNAME=admin
      - MONGO_INITDB_ROOT_PASSWORD=password
  cloudband:
    image: cloudband
    container_name: cloudband
    ports:
      - 3000:3000
    command: npm run dev:server
networks:
  app:

I expected my application and mongo db to start running in their respective containers and for them to be able to communicate (i.e. create documents / find documents / etc.).

What I have already tried: -making sure they are in the same network (they are) -making sure they can ping each other (they can) -adding links to my app in the yaml file -checked configurations and i think they are ok (port, host, ip) -switching my uri to the following things:

# MONGO_URI_=mongodb://admin:password@localhost:27017/dbname
MONGO_URI_=mongodb://localhost:27017/dbname
# MONGO_URI_=mongodb://127.0.0.1:27017/dbname

Things to consider:

  • node v18.12.0 is installed on my computer

CodePudding user response:

In a container, localhost means the container itself.

Docker-compose creates a docker network where the containers can talk to each other using their service name or container names as host names.

So, instead of

MONGO_URI_=mongodb://localhost:27017/dbname

you need to use

MONGO_URI_=mongodb://mongo:27017/dbname

CodePudding user response:

Literally saving lives, Hans. I am in your debt for this kindness! Thank you!

  • Related