【问题标题】:I have starting using Docker with Stack application and I'm getting this error .MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017我已开始将 Docker 与 Stack 应用程序一起使用,但出现此错误 .MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017
【发布时间】:2021-12-22 19:21:39
【问题描述】:

我从星期一开始学习 docker,但我遇到了错误

这是 docker-compose.yml 代码

version: '3.9'

services:
  mongo:
    container_name: mongo
    image: mongo:latest
    restart: always
    ports:
      - 27017:27017
    volumes:
      - mongo_db:/data/db
  server:
    container_name: server
    restart: always
    build: ./server
    ports:
      - 5500:5500
    depends_on:
      - mongo

  client:
    container_name: client
    restart: always
    build: ./client
    ports:
      - 3000:3000
    depends_on:
      - server

volumes:
  mongo_db : {}

Dockerfiles

服务器

FROM node:17-alpine

# make App Directory
WORKDIR /server

# Install Dependencies
COPY package*.json /.

RUN npm ci 

# Copy the current directory contents into the container at /client
COPY . .

# Exports
EXPOSE 5500

CMD ["npm","start"]

还有我的代码

const mongoose = require("mongoose");
mongoose
  .connect("mongodb://mongo:27017/blogapp", {
    useNewUrlParser: true,
    useUnifiedTopology: true,
  })
  .then(() => {
    console.log("connection success");
  })
  .catch((err) => {
    console.log(err);
  });

const blogSchema = new mongoose.Schema({
  title: {
    type: String,
    required: true,
  },
  description: String,
  author: String
});

const Comment = new mongoose.model("Comment", blogSchema);

const Blog = new mongoose.model("blog", blogSchema);

const reactBlog = new Blog({
    title: "1st Blog",
      description: "This is  our first blog",
      author: "Aakash",
     
})

const userSchema = new mongoose.Schema({
    first_name: { type: String, default: null },
    last_name: { type: String, default: null },
    email: { type: String, unique: true },
    password: { type: String },
    city:{type: String},
    token: { type: String },
  });
  
  module.exports = mongoose.model("user", userSchema);
  });

发生了这个错误

服务器在端口 5500 上运行 服务器 | MongooseServerSelectionError:连接 ECONNREFUSED 127.0.0.1:27017 服务器 |在 NativeConnection.Connection.openUri (/node_modules/mongoose/lib/connection.js:796:32) 服务器 |在 /node_modules/mongoose/lib/index.js:330:10 服务器 |在 /node_modules/mongoose/lib/helpers/promiseOrCallback.js:32:5 服务器
|在新的 Promise () 服务器 |在 承诺或回调 (/node_modules/mongoose/lib/helpers/promiseOrCallback.js:31:10) 服务器 |在 Mongoose._promiseOrCallback (/node_modules/mongoose/lib/index.js:1151:10) 服务器 |在 Mongoose.connect (/node_modules/mongoose/lib/index.js:329:20) 服务器
|在 Object.exports.connect (/server/config/database.js:7:4) 服务器 |在对象。 (/server/index.js:2:30) 服务器
|在 Module._compile (node:internal/modules/cjs/loader:1095:14) { 服务器 |原因:拓扑描述 { 服务器 |类型: '未知',服务器 |服务器:地图(1){'localhost:27017'=> [服务器描述] },服务器 |陈旧:假,服务器 |
兼容:真实,服务器 | heartbeatFrequencyMS:10000,服务器 | localThresholdMS:15,服务器 |
logicalSessionTimeoutMinutes:未定义的服务器 | } 服务器 | }

【问题讨论】:

标签: node.js mongodb docker


【解决方案1】:

您必须使用 mongoDB 服务名称更改您的数据库 URL。在您的情况下,服务名称是mongo。试试下面的代码。

const mongoose = require("mongoose");
mongoose.connect("mongo://mongo:27017/blogapp", {
    useNewUrlParser: true,
    useUnifiedTopology: true,
  }).then(() => {
    console.log("connection success");
  }).catch((err) => {
    console.log(err);
  })

【讨论】:

  • 尝试不工作我得到同样的错误
  • 您在更改代码后尝试docker-compose up --build 吗?
  • 是的,我已经尝试过了,我已经删除了基于该图像构建的所有容器并再次尝试,但没有任何效果。
  • 这看起来和问题的代码一样
  • 原码有这个mongodb://mongo:27017/blogapp
猜你喜欢
  • 2021-04-15
  • 2023-02-24
  • 1970-01-01
  • 2022-11-23
  • 2021-11-22
  • 1970-01-01
  • 2021-12-18
  • 2021-09-01
  • 2016-03-02
相关资源
最近更新 更多