Mongoose Server Selection Error ECONNREFUSED with a docker-compose

Viewed 4485

I'm trying to create a minimal dockerised Mongo Express React Node stack. Mongoose connects fine to my dockerised mongo when using node, but fails when trying inside docker.

back.js :

const express = require('express');
const app = express();
const mongoose = require('mongoose');

mongoose.connect('mongodb://mongo:27017/test', {useNewUrlParser: true, useUnifiedTopology: true},
    () => {console.log("connection established")})
    .catch(error => handleError(error));
let db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {

// some stuff

    });

    app.listen(3000, function () {
        console.log('Express-js listening on port 3000')
    });
});

docker-compose.yml :

version: "3.7"

services:
  mongodb:
    image: mongo:latest
    container_name: mongo
    volumes: 
      - mongodb-data:/data/db
    ports:
      - 27017:27017

  express-js:
    build: ./back-express/ #local Dockerfile
    ports:
      - 3000:3000
    depends_on:
      - mongodb

volumes: 
  mongodb-data:

Dockerfile :

FROM node:lts
WORKDIR ./
COPY package.json yarn.lock ./
RUN yarn install --production
COPY . .
CMD ["node", "back.js"]

Error :

express-js_1     | connection error: MongooseError [MongooseServerSelectionError]: connect ECONNREFUSED 127.0.0.1:27017
express-js_1     |     at new MongooseServerSelectionError (/node_modules/mongoose/lib/error/serverSelection.js:22:11)

(...)

express-js_1     |   message: 'connect ECONNREFUSED 127.0.0.1:27017',
express-js_1     |   name: 'MongooseServerSelectionError',
express-js_1     |   reason: TopologyDescription {
express-js_1     |     type: 'Single',
express-js_1     |     setName: null,
express-js_1     |     maxSetVersion: null,
express-js_1     |     maxElectionId: null,
express-js_1     |     servers: Map { 'localhost:27017' => [ServerDescription] },
express-js_1     |     stale: false,
express-js_1     |     compatible: true,
express-js_1     |     compatibilityError: null,
express-js_1     |     logicalSessionTimeoutMinutes: null,
express-js_1     |     heartbeatFrequencyMS: 10000,
express-js_1     |     localThresholdMS: 15,
express-js_1     |     commonWireVersion: null
express-js_1     |   },
express-js_1     |   [Symbol(mongoErrorContextSymbol)]: {}
express-js_1     | }

I tried to add this before the last line in my Dockerfile : CMD ["sleep", "10"] but it had no effect.

I have no idea what's going wrong. I've spent a long time searching the Internet. Any help would make my day.

Full code accessible here : https://github.com/npasquie/back-express clone this project inside the first : https://github.com/npasquie/back-express

1 Answers

A friend of mine find the answer. The code works but I forgot to rebuild my images with docker-compose build before launching the containers.

Sorry for the original question not being really useful, still I think I won't delete it because it may help other beginners who made the same mistake.

Related