gpt4 book ai didi

node.js - 无法从 M1 Mac 上的 Node 容器连接到 Mongo Docker 容器

转载 作者:行者123 更新时间:2023-12-05 03:25:03 24 4
gpt4 key购买 nike

我目前正在尝试使用 M1 芯片将我的 Node 容器连接到我的 Mongo 容器。许多人说您不能在 M1 Mac 上运行 Mongo 4.9+,因为它不支持 AVR。我不能说为什么,但在我的 M1 mongo 上运行得很好。我可以使用 mongo://mongo:27017 通过 Mongo Compass 连接到容器。此外,如果我在 docker 之外运行我的 Node 应用程序,我可以使用相同的连接字符串连接到 Mongo 容器。但出于某种原因,我无法将容器化 Node 应用程序连接到容器化 Mongo 服务。

docker 文件

 FROM node:12

# Create app directory
WORKDIR /usr/src/app

# Copy dependencies
COPY package*.json ./

# Install dependencies
RUN npm install

RUN npm ci --only=production

# Bundle app source
COPY . .

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

docker-compose.yml

version: "2"
services:
web:
build: .
ports:
- "3000:3000"
depends_on:
- mongo
mongo:
image: mongo
ports:
- "27017:27017"

连接方式

/* Mongoose Connection */
const mongoose = require('mongoose')

mongoose.Promise = global.Promise
mongoose.connect(
'mongodb://mongo:27017',
{ useNewUrlParser: true }
)
mongoose.connection.on('error', console.error.bind(console, 'MongoDB connection

错误:'))

mongoose.set('debug', true)

module.exports = mongoose.connection

服务器.js

require('dotenv').config();
const express = require('express')
const app = express()
const bodyParser = require('body-parser')
const expressValidator = require('express-validator')
var cookieParser = require('cookie-parser');
const jwt = require('jsonwebtoken');
app.use(cookieParser()); // Add this after you initialize express.


// db
require('./data/reddit-db')

// set db
const exphbs = require('express-handlebars')

// body parser
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))
app.use(expressValidator())

app.engine('handlebars', exphbs({ defaultLayout: 'main' }))
app.set('view engine', 'handlebars')


var checkAuth = (req, res, next) => {
console.log("Checking authentication");
if (typeof req.cookies.nToken === "undefined" || req.cookies.nToken === null) {
req.user = null;
} else {
var token = req.cookies.nToken;
var decodedToken = jwt.decode(token, { complete: true }) || {};
req.user = decodedToken.payload;
}

next();
};
app.use(checkAuth);

// routes
require('./controllers/posts.js')(app)

require('./controllers/comments.js')(app)

require('./controllers/auth.js')(app)

require('./controllers/replies.js')(app)




// Start Server
app.listen(3000, () => {
console.log('Reddit Search listening on port localhost:3000!');
});

module.exports = app

错误

redditjspart2-web-1    | Reddit Search listening on port localhost:3000!
redditjspart2-web-1 | MongoDB connection Error: { MongoNetworkError: failed to connect to server [127.0.0.1:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017]
redditjspart2-web-1 | at Pool.<anonymous> (/usr/src/app/node_modules/mongoose/node_modules/mongodb/lib/core/topologies/server.js:438:11)
redditjspart2-web-1 | at Pool.emit (events.js:198:13)
redditjspart2-web-1 | at createConnection (/usr/src/app/node_modules/mongoose/node_modules/mongodb/lib/core/connection/pool.js:561:14)
redditjspart2-web-1 | at connect (/usr/src/app/node_modules/mongoose/node_modules/mongodb/lib/core/connection/pool.js:994:11)
redditjspart2-web-1 | at makeConnection (/usr/src/app/node_modules/mongoose/node_modules/mongodb/lib/core/connection/connect.js:31:7)
redditjspart2-web-1 | at callback (/usr/src/app/node_modules/mongoose/node_modules/mongodb/lib/core/connection/connect.js:264:5)
redditjspart2-web-1 | at Socket.err (/usr/src/app/node_modules/mongoose/node_modules/mongodb/lib/core/connection/connect.js:294:7)
redditjspart2-web-1 | at Object.onceWrapper (events.js:286:20)
redditjspart2-web-1 | at Socket.emit (events.js:198:13)
redditjspart2-web-1 | at emitErrorNT (internal/streams/destroy.js:91:8)
redditjspart2-web-1 | at emitErrorAndCloseNT (internal/streams/destroy.js:59:3)
redditjspart2-web-1 | at process._tickCallback (internal/process/next_tick.js:63:19)
redditjspart2-web-1 | name: 'MongoNetworkError',
redditjspart2-web-1 | [Symbol(mongoErrorContextSymbol)]: {} }

我尝试过的:

  • 在英特尔 mac 上运行相同的 repo(完美运行)
  • 更改连接字符串 IP (mongo, localhost, 127.0.0.1)
  • 使用带有网桥驱动程序的 docker 网络并在连接字符串中使用分配的 ip
  • 从 Node 容器内 ping mongo(这有效,所有数据包都收到了)

错误表明它正在尝试连接到 127.0.0.1:27017,这意味着它正在正确解析主机名并将其映射到我的本地主机。所以Node容器是肯定能找到的。这也是有道理的,因为我仍然可以像前面提到的那样从 Node 容器 ping Mongo 容器。看起来 Mongo 只是出于某种原因拒绝连接。

我真的很难弄清楚为什么会这样。我的 repo 的链接是公开的,你可以在 github 上找到它:https://github.com/lukeaparker/reddit.jspart2

非常感谢!!

最佳答案

我在 M1 Mac 上遇到了类似的问题,并通过将这些配置放入 docker-compose.yml 中设法解决了这个问题:

services:
mongodb:
image: arm64v8/mongo:4.0
platform: linux/arm64/v8

我还通过 CLI 使用 docker build --platform linux/arm64/v8docker run --platform linux/arm64/v8 来实现平台明确性,因为我得到了这个先前警告:

WARNING: The requested image's platform (linux/amd64) does not match the detected host platform (linux/arm64/v8) and no specific platform was requested

关于node.js - 无法从 M1 Mac 上的 Node 容器连接到 Mongo Docker 容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72122341/

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