草庐IT

node.js - NodeJS/Express 无法连接到在 docker 中运行的 mongodb

coder 2023-10-31 原文

我在从 nodejs/express 应用程序连接到在 docker 容器中运行的 mongodb 实例时遇到问题。 nodejs 应用程序在我的本地主机 Mac OS X 上运行。我正在使用 Docker 工具箱在我的 Mac 上运行 docker,而不是 boot2docker。

我已经通过 -p arg 启用端口转发到 docker run。

docker run -p 27017:27017 --name test -d mongo:2.6
60b7f3cf658bc75ceca2eae074e5f694ec622f7487b6a2f36fa02edd4af68357

我可以看到它在运行。

docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                      NAMES
60b7f3cf658b        mongo:2.6           "/entrypoint.sh mongo"   9 minutes ago       Up 9 minutes        0.0.0.0:27017->27017/tcp   test

如果我运行 docker logs,我会看到它正在监听端口 27017。

docker logs test
mongod --help for help and startup options
2015-10-03T15:42:25.833+0000 [initandlisten] MongoDB starting : pid=1 port=27017 dbpath=/data/db 64-bit host=60b7f3cf658b
2015-10-03T15:42:25.833+0000 [initandlisten] db version v2.6.11
2015-10-03T15:42:25.833+0000 [initandlisten] git version: d00c1735675c457f75a12d530bee85421f0c5548
2015-10-03T15:42:25.833+0000 [initandlisten] build info: Linux build4.ny.cbi.10gen.cc 2.6.32-431.3.1.el6.x86_64 #1 SMP Fri Jan 3 21:39:27 UTC 2014 x86_64 BOOST_LIB_VERSION=1_49
2015-10-03T15:42:25.833+0000 [initandlisten] allocator: tcmalloc
2015-10-03T15:42:25.833+0000 [initandlisten] options: {}
2015-10-03T15:42:25.835+0000 [initandlisten] journal dir=/data/db/journal
2015-10-03T15:42:25.835+0000 [initandlisten] recover : no journal files present, no recovery needed
2015-10-03T15:42:25.860+0000 [initandlisten] allocating new ns file /data/db/local.ns, filling with zeroes...
2015-10-03T15:42:25.891+0000 [FileAllocator] allocating new datafile /data/db/local.0, filling with zeroes...
2015-10-03T15:42:25.891+0000 [FileAllocator] creating directory /data/db/_tmp
2015-10-03T15:42:25.892+0000 [FileAllocator] done allocating datafile /data/db/local.0, size: 64MB,  took 0 secs
2015-10-03T15:42:25.893+0000 [initandlisten] build index on: local.startup_log properties: { v: 1, key: { _id: 1 }, name: "_id_", ns: "local.startup_log" }
2015-10-03T15:42:25.893+0000 [initandlisten]     added index to empty collection
2015-10-03T15:42:25.894+0000 [initandlisten] waiting for connections on port 27017

当我尝试从 express 连接时,出现以下错误。

my-app/node_modules/mongodb/lib/server.js:228
        process.nextTick(function() { throw err; })
                                            ^
Error: connect ECONNREFUSED
    at exports._errnoException (util.js:746:11)
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1010:19)

现在,stackoverflow 上还有其他帖子引用了这件事。他们都说“通过-p 参数启用端口转发”,我做到了。但它仍然不允许我连接。

我正在使用正确的端口和主机 VM IP - 我可以通过常规的 mongo shell 工具从我的 Mac 连接到 mongodb 容器。

还有一些奇怪的事情......

  • 我可以通过 mongo shell 从我的 Mac 连接到容器 mongodb 就好了。当我运行 docker logs test 时显示连接。
  • 我也在日志中看到来 self 的 nodejs 应用程序的连接,但随后立即断开连接。


2015-10-03T15:53:25.938+0000 [clientcursormon]  connections:0
2015-10-03T15:54:08.843+0000 [initandlisten] connection accepted from 192.168.99.1:54804 #1 (1 connection now open)
2015-10-03T15:54:08.855+0000 [conn1] end connection 192.168.99.1:54804 (0 connections now open)

另见:

最佳答案

我将回答我自己的问题但不接受它。所以事实证明错误是由另一部分隐藏的 nodejs 代码引起的,该代码试图连接到 mongodb://127.0.0.1:27017。此 mongodb 实例不存在,因此抛出错误。

我集中了 mongodb 配置,一切都很顺利。

关于node.js - NodeJS/Express 无法连接到在 docker 中运行的 mongodb,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32924557/

有关node.js - NodeJS/Express 无法连接到在 docker 中运行的 mongodb的更多相关文章

  1. ruby-on-rails - 由于 "wkhtmltopdf",PDFKIT 显然无法正常工作 - 2

    我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-

  2. ruby-on-rails - 无法使用 Rails 3.2 创建插件? - 2

    我对最新版本的Rails有疑问。我创建了一个新应用程序(railsnewMyProject),但我没有脚本/生成,只有脚本/rails,当我输入ruby./script/railsgeneratepluginmy_plugin"Couldnotfindgeneratorplugin.".你知道如何生成插件模板吗?没有这个命令可以创建插件吗?PS:我正在使用Rails3.2.1和ruby​​1.8.7[universal-darwin11.0] 最佳答案 随着Rails3.2.0的发布,插件生成器已经被移除。查看变更日志here.现在

  3. ruby - 无法运行 Rails 2.x 应用程序 - 2

    我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby​​:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r

  4. ruby-on-rails - 无法在centos上安装therubyracer(V8和GCC出错) - 2

    我正在尝试在我的centos服务器上安装therubyracer,但遇到了麻烦。$geminstalltherubyracerBuildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingtherubyracer:ERROR:Failedtobuildgemnativeextension./usr/local/rvm/rubies/ruby-1.9.3-p125/bin/rubyextconf.rbcheckingformain()in-lpthread...yescheckingforv8.h...no***e

  5. ruby - 无法让 RSpec 工作—— 'require' : cannot load such file - 2

    我花了三天的时间用头撞墙,试图弄清楚为什么简单的“rake”不能通过我的规范文件。如果您遇到这种情况:任何文件夹路径中都不要有空格!。严重地。事实上,从现在开始,您命名的任何内容都没有空格。这是我的控制台输出:(在/Users/*****/Desktop/LearningRuby/learn_ruby)$rake/Users/*******/Desktop/LearningRuby/learn_ruby/00_hello/hello_spec.rb:116:in`require':cannotloadsuchfile--hello(LoadError) 最佳

  6. ruby - 无法覆盖 irb 中的 to_s - 2

    我在pry中定义了一个函数:to_s,但我无法调用它。这个方法去哪里了,怎么调用?pry(main)>defto_spry(main)*'hello'pry(main)*endpry(main)>to_s=>"main"我的ruby版本是2.1.2看了一些答案和搜索后,我认为我得到了正确的答案:这个方法用在什么地方?在irb或pry中定义方法时,会转到Object.instance_methods[1]pry(main)>defto_s[1]pry(main)*'hello'[1]pry(main)*end=>:to_s[2]pry(main)>defhello[2]pry(main)

  7. ruby - 无法在 60 秒内获得稳定的 Firefox 连接 (127.0.0.1 :7055) - 2

    我使用的是Firefox版本36.0.1和Selenium-Webdrivergem版本2.45.0。我能够创建Firefox实例,但无法使用脚本继续进行进一步的操作无法在60秒内获得稳定的Firefox连接(127.0.0.1:7055)错误。有人能帮帮我吗? 最佳答案 我遇到了同样的问题。降级到firefoxv33后一切正常。您可以找到旧版本here 关于ruby-无法在60秒内获得稳定的Firefox连接(127.0.0.1:7055),我们在StackOverflow上找到一个类

  8. ruby - 安装 Ruby 时遇到问题(无法下载资源 "readline--patch") - 2

    当我尝试安装Ruby时遇到此错误。我试过查看this和this但无济于事➜~brewinstallrubyWarning:YouareusingOSX10.12.Wedonotprovidesupportforthispre-releaseversion.Youmayencounterbuildfailuresorotherbreakages.Pleasecreatepull-requestsinsteadoffilingissues.==>Installingdependenciesforruby:readline,libyaml,makedepend==>Installingrub

  9. ruby-on-rails - 无法让 rspec、spork 和调试器正常运行 - 2

    GivenIamadumbprogrammerandIamusingrspecandIamusingsporkandIwanttodebug...mmm...let'ssaaay,aspecforPhone.那么,我应该把“require'ruby-debug'”行放在哪里,以便在phone_spec.rb的特定点停止处理?(我所要求的只是一个大而粗的箭头,即使是一个有挑战性的程序员也能看到:-3)我已经尝试了很多位置,除非我没有正确测试它们,否则会发生一些奇怪的事情:在spec_helper.rb中的以下位置:require'rubygems'require'spork'

  10. ruby-on-rails - capybara ::ElementNotFound:无法找到 xpath "/html" - 2

    我正在学习http://ruby.railstutorial.org/chapters/static-pages上的RubyonRails教程并遇到以下错误StaticPagesHomepageshouldhavethecontent'SampleApp'Failure/Error:page.shouldhave_content('SampleApp')Capybara::ElementNotFound:Unabletofindxpath"/html"#(eval):2:in`text'#./spec/requests/static_pages_spec.rb:7:in`(root)'

随机推荐