gpt4 book ai didi

node.js - docker : MongooseError [MongooseServerSelectionError]: getaddrinfo ENOTFOUND mongo

转载 作者:行者123 更新时间:2023-12-02 18:13:13 49 4
gpt4 key购买 nike

我正在尝试将 mongodb 与我在 docker 容器中的应用程序连接起来。我正在使用 Mongoose 包,这是我写的代码

mongoose.connect("mongodb://mongo:27016/IssueTracker", { useNewUrlParser: true,useUnifiedTopology: true },(err: Error) => {
err ? console.log(err) : console.log("Mongodb database connected");
});

这是我的 docker-compose.yml文件
version: "3"
services:
app:
container_name: IssueTracker
restart: always
build: .
ports:
- '9000:9000'
links:
- mongo
mongo:
container_name: mongo
image: mongo
ports:
- '27017:27016'

这是我的 Dockerfile
FROM node:13

WORKDIR /app

COPY package*.json ./

RUN npm install

COPY . .

EXPOSE 9000

RUN ["npm","run","dev"]

这是我尝试运行时遇到的错误 docker-compose up在我的 docker cmdline 中
server started on port 9000
MongooseError [MongooseServerSelectionError]: getaddrinfo ENOTFOUND mongo
at new MongooseServerSelectionError (/app/node_modules/mongoose/lib/error/serverSelection.js:22:11)
at NativeConnection.Connection.openUri (/app/node_modules/mongoose/lib/connection.js:823:32)
at Mongoose.connect (/app/node_modules/mongoose/lib/index.js:333:15)
at Object.<anonymous> (/app/src/app.ts:9:10)
at Module._compile (internal/modules/cjs/loader.js:1123:30)
at Module.m._compile (/app/node_modules/ts-node/src/index.ts:839:23)
at Module._extensions..js (internal/modules/cjs/loader.js:1143:10)
at Object.require.extensions.<computed> [as .ts] (/app/node_modules/ts-node/src/index.ts:842:12)
at Module.load (internal/modules/cjs/loader.js:972:32)
at Function.Module._load (internal/modules/cjs/loader.js:872:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
at main (/app/node_modules/ts-node/src/bin.ts:227:14)
at Object.<anonymous> (/app/node_modules/ts-node/src/bin.ts:513:3)
at Module._compile (internal/modules/cjs/loader.js:1123:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1143:10)
at Module.load (internal/modules/cjs/loader.js:972:32) {
reason: TopologyDescription {
type: 'Single',
setName: null,
maxSetVersion: null,
maxElectionId: null,
servers: Map(1) { 'mongo:27016' => [ServerDescription] },
stale: false,
compatible: true,
compatibilityError: null,
logicalSessionTimeoutMinutes: null,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
commonWireVersion: null
},
[Symbol(mongoErrorContextSymbol)]: {}
}

试图检查我在 docker 中的任何容器是否正在使用提到的端口,以便可能会产生此错误,并且没有人使用相同的端口。我是 docker 新手,所以不太了解它,我尝试查看一些在线解决方案,但没有帮助

最佳答案

尝试创建和使用网络而不是 link

version: "3"
services:
app:
container_name: IssueTracker
restart: always
build: .
ports:
- '9000:9000'
networks: #here
- my-network
mongo:
container_name: mongo
image: mongo
ports:
- '27017:27016'
networks: #here
- my-network

networks: #here
my-network:
driver: bridge

关于node.js - docker : MongooseError [MongooseServerSelectionError]: getaddrinfo ENOTFOUND mongo,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61539538/

49 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com