简体   繁体   English

无法从Dockerized节点程序连接到Localhost Mongodb

[英]Unable to Connect From Dockerized Node Program to Localhost Mongodb

I am able to npm start the code from cmd and have it running with mongodb but docker build the code and docker run it generates this error: 我能够npm从cmd启动代码,并使其与mongodb一起运行,但是docker构建代码,然后docker运行它会生成此错误:

/usr/src/app/node_modules/mongoose/node_modules/mongodb/lib/server.js:265
        process.nextTick(function() { throw err; })
                                      ^
MongoError: failed to connect to server [127.0.0.1:27017] on first connect
    at null.<anonymous> (/usr/src/app/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:325:35)
    at emitOne (events.js:77:13)
    at emit (events.js:169:7)
    at null.<anonymous> (/usr/src/app/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/connection/pool.js:270:12)
    at g (events.js:260:16)
    at emitTwo (events.js:87:13)
    at emit (events.js:172:7)
    at Socket.<anonymous> (/usr/src/app/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/connection/connection.js:173:49)
    at Socket.g (events.js:260:16)
    at emitOne (events.js:77:13)
    at Socket.emit (events.js:169:7)
    at emitErrorNT (net.js:1269:8)
    at nextTickCallbackWith2Args (node.js:458:9)
    at process._tickCallback (node.js:372:17)

My app.js is listening in on port 8080 and i exposed port 8080 and 27017 on dockerfile. 我的app.js在8080端口上监听,我在dockerfile上暴露了8080和27017端口。 May i know what else should i be doing to resolve the error. 我可以知道我还应该做些什么来解决该错误。 I am a beginner on this, pardon me. 我是这个初学者,请原谅。

you only expose 8080 and that's why it couldn't connect to 27017 ! 您只暴露8080 ,这就是为什么它无法连接到27017 instead of -p 8080:8080 use -P to expose 27017 too! 代替-p 8080:8080 ,也要使用-P公开27017! so you're build command would be this: 所以您要构建的命令是这样的:

docker run -P -d ryan/edocker

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM