使用Express部署Docker容器,Mongoose错误:getaddrinfo ENOTFOUND mongo

i1icjdpr  于 2023-04-21  发布在  Go
关注(0)|答案(1)|浏览(217)

我正在尝试在Cloud Run上启动我的第一个容器。我有一个使用MongoDB的Node.js服务器。我搜索了一下,并设法在本地网络上启动容器,没有任何问题。一旦我继续上传,服务器启动,但从我猜测服务器无法连接到MongoDB。
这是我在控制台得到的错误

pr 21 01:12:30 AM  MongoServerSelectionError: getaddrinfo ENOTFOUND mongo
Apr 21 01:12:30 AM      at Timeout._onTimeout (/opt/render/project/src/node_modules/mongodb/lib/sdam/topology.js:277:38)
Apr 21 01:12:30 AM      at listOnTimeout (internal/timers.js:555:17)
Apr 21 01:12:30 AM      at processTimers (internal/timers.js:498:7) {
Apr 21 01:12:30 AM    reason: TopologyDescription {
Apr 21 01:12:30 AM      type: 'Unknown',
Apr 21 01:12:30 AM      servers: Map(1) { 'mongo:27017' => [ServerDescription] },
Apr 21 01:12:30 AM      stale: false,
Apr 21 01:12:30 AM      compatible: true,
Apr 21 01:12:30 AM      heartbeatFrequencyMS: 10000,
Apr 21 01:12:30 AM      localThresholdMS: 15,
Apr 21 01:12:30 AM      setName: null,
Apr 21 01:12:30 AM      maxElectionId: null,
Apr 21 01:12:30 AM      maxSetVersion: null,
Apr 21 01:12:30 AM      commonWireVersion: 0,
Apr 21 01:12:30 AM      logicalSessionTimeoutMinutes: null
Apr 21 01:12:30 AM    },
Apr 21 01:12:30 AM    code: undefined,
Apr 21 01:12:30 AM    [Symbol(errorLabels)]: Set(0) {}
Apr 21 01:12:30 AM  }
Apr 21 01:12:30 AM  (node:75) UnhandledPromiseRejectionWarning: MongooseServerSelectionError: getaddrinfo ENOTFOUND mongo
Apr 21 01:12:30 AM      at _handleConnectionErrors (/opt/render/project/src/node_modules/mongoose/lib/connection.js:755:11)
Apr 21 01:12:30 AM      at NativeConnection.openUri (/opt/render/project/src/node_modules/mongoose/lib/connection.js:730:11)
Apr 21 01:12:30 AM  (Use `node --trace-warnings ...` to show where the warning was created)
Apr 21 01:12:30 AM  (node:75) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)

这是我的Dockerfile

FROM node

WORKDIR /usr/src/app

COPY package*.json ./
RUN npm install
COPY . .

EXPOSE 8080

CMD [ "node", "server.js" ]

这是docker-compose

version: "3"
services:
  web:
    build: .
    ports:
      - "8080:8080"
    external_links:
       - "mongo"

  mongo:
  container_name: mongo
  image: mongo
  volumes:
  - ./data:/data/db
  ports:
  - '27012:27017'

当我连接到MongoDB时,这是我运行的代码

mongoose.connect('mongodb://mongo:27017/toDoBeta',  {
    useNewUrlParser: true,
    })

有没有人有任何关于如何解决这个问题的提示/想法?非常感谢你的帮助和调查这个问题。

w1e3prcc

w1e3prcc1#

端口Map不正确,因为您连接到27017,并且您将27012暴露为要Map到27012

ports: 
  - 27017:27017

顺便说一句,压痕是问题在yaml

相关问题