简体   繁体   English

Mongoose 服务器选择错误 ECONNREFUSED 与 docker-compose

[英]Mongoose Server Selection Error ECONNREFUSED with a docker-compose

I'm trying to create a minimal dockerised Mongo Express React Node stack.我正在尝试创建一个最小的 dockerised Mongo Express React Node 堆栈。 Mongoose connects fine to my dockerised mongo when using node, but fails when trying inside docker. Mongoose 在使用 node 时可以很好地连接到我的 dockerised mongo,但在 docker 内部尝试时失败。

back.js :返回.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 : 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 : 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.我试图在我的 Dockerfile 的最后一行之前添加这个: CMD ["sleep", "10"]但它没有效果。

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完整代码可在此处访问: https : //github.com/npasquie/back-express在第一个中克隆此项目: https : //github.com/npasquie/back-express

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.该代码有效,但我忘记在启动容器之前使用docker-compose build重建我的图像。

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.很抱歉最初的问题不是很有用,但我仍然认为我不会删除它,因为它可能会帮助其他犯同样错误的初学者。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM