Node / Express应用程序无法连接到docker mongodb [英] Node / Express app can't connect to docker mongodb

查看:194
本文介绍了Node / Express应用程序无法连接到docker mongodb的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想运行一个节点应用程序,它使用express并连接到(boot2docker)docker mongo容器。

I want to run a node app that uses express and connects to a (boot2docker) docker mongo container.

当我第一次写这个应用程序时,我正在使用mongodb的本地安装实例,并且以下配置工作:

When I first wrote the app, I was using a locally installed instance of mongodb, and the following config worked:

module.exports = {
  env: 'development',
  mongo: {
    uri: 'mongodb://localhost/fullstack-dev'
  }
};

它按预期运行。

现在我试图换掉一个docker的mongo实例。

Now I'm trying to swap that out for a docker instance of mongo.

所以,我已经完成了以下步骤来让mongo运行:

So, I've done the following steps to get mongo running:

$ docker pull mongo:latest
$ docker run -v "$(pwd)":/data --name mongo -d mongo mongod --smallfiles
$ docker ps

#my mongo instance is 442c2541fe1a
$ docker exec -it 442c2541fe1a bash
$ mongo

此时,似乎在我的命令提示符下运行。

At this point, appears to be running in my command prompt.

然后,我试图获取在osx上运行docker的boot2docker vm的IP:

Then, I tried to get the IP of boot2docker vm that runs docker on osx:

$ boot2docker ip
# the IP returned: 192.168.59.103

那么,那么,我去把nodejs中的旧mongodb路径配置如下:

So, then, I went to swap out the old mongodb path in the nodejs config to the following:

module.exports = {
  env: 'development',
  mongo: {
    uri: 'mongodb://192.168.59.103:27017/fullstack-dev'
  }
};

当我运行应用程序时,我收到连接错误。

and when I run the application, I get a connection error.

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: failed to connect to [192.168.59.103:27017]

什么是正确的配置连接我的MEAN堆栈应用程序与docker'ified mongodb实例?

What is the proper config for connecting my MEAN stack application with a docker'ified mongodb instance?

推荐答案

p>您需要使用 -p 参数在容器上发布适当的端口,即:

You need to publish the appropriate port on the container with the -p argument i.e:

docker run -p 27017:27017 -v "$(pwd)":/data --name mongo -d mongo mongod --smallfiles

将使端口27017可以在主机(您的情况下的VM)上访问,并将流量转发到容器上的端口27017。

The will make port 27017 accessible on the host (the VM in your case) and forward traffic to port 27017 on the container.

这篇关于Node / Express应用程序无法连接到docker mongodb的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆