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

时间:2016-12-21 01:45:46

标签: node.js mongodb docker mongoose

我能够从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)

我的app.js正在侦听端口8080,我在dockerfile上暴露了端口8080和27017。我可以知道我还应该做些什么来解决这个错误。我是初学者,请原谅我。

1 个答案:

答案 0 :(得分:0)

您只展示8080以及它无法连接27017的原因! 而不是-p 8080:8080使用-P来暴露27017! 所以你构建命令就是这样:

docker run -P -d ryan/edocker