我目前正在尝试使用M1芯片将我的Node容器连接到Mongo容器。很多人都说你不能在M1 Mac上运行Mongo 4.9+,因为它不支持AVR。我不能说为什么,但在我的M1 Mongo上运行得很好。我可以使用mongo://mongo:27017通过Mongo Compass连接到容器。此外,如果我在docker之外运行我的Node应用程序,我可以使用相同的连接字符串连接到Mongo容器。但由于某种原因,我不能将容器化的节点应用程序连接到容器化的Mongo服务。
停靠文件
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
server.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)]: {} }
我尝试过的:
- 在intel mac上运行相同的repo(工作完美)
- 更改连接字符串IP(mongo、localhost、127.0.0.1)
- 使用带有网桥驱动程序的Docker网络并使用连接字符串中分配的ip
- 从节点容器内部ping mongo(此操作有效且所有数据包均已接收)
这个错误说它正在尝试连接到127.0.0.1:27017,这意味着它正确地解析了主机名并将其Map到我的localhost。所以Node容器肯定能找到它。这也是有意义的,因为我仍然可以从Node容器ping Mongo容器,就像我前面提到的那样。看起来Mongo只是出于某种原因拒绝连接。
我真的很难弄清楚为什么会这样。我的回购协议的链接是公开的,你可以在github上找到它:https://github.com/lukeaparker/reddit.jspart2
非常感谢!!
3条答案
按热度按时间ycggw6v21#
我有一个类似的问题与M1的Mac和设法解决它把这些配置在docker-compose.yml:
我还通过CLI使用
docker build --platform linux/arm64/v8
和docker run --platform linux/arm64/v8
来明确平台,因为我之前收到了以下警告:警告:请求的映像平台(linux/amd 64)与检测到的主机平台(linux/arm 64/v8)不匹配,并且未请求特定平台
yebdmbv42#
我也有同样的问题。
正如我所看到的问题只是与mongo 5+。
我在试着把蒙戈的图像降级到
image: mongo:4.4.14
7lrncoxx3#
对于https://jira.mongodb.org/browse/SERVER-50115,MongoDB在为Mac M1/M2构建映像之前需要一些依赖关系。
根据MongoDB工程师,尝试在M1上的Docker中运行Linux x86_64二进制文件,这将是一个问题,因为Linux x86_64的MongoDB 5.0包中需要AVX支持。最初问题的解决方案是在Linux上安装MongoDB 4.4包(因为它们没有针对AVX进行优化)或使用旧的x86_64 CPU架构目标从源代码构建MongoDB 5.x。