从Mac上的本地docker镜像连接到meteor mongo

时间:2015-11-16 14:15:10

标签: mongodb meteor docker

meteor mongo -U向我显示mongodb://127.0.0.1:3001/meteor作为连接字符串。我知道docker镜像会尝试连接到自身并失败。我尝试用127.0.0.1代替运行ifconfig时可以找到的任何IP地址,结果相同:

{ [MongoError: connect ECONNREFUSED xxx.xxx.xxx.xxx:3001]
  name: 'MongoError',
  message: 'connect ECONNREFUSED xxx.xxx.xxx.xxx:3001' }

不确定我还能尝试解决此问题。任何指针都将受到赞赏。

更多信息: mongo 127.0.0.1:3001/meteor来自不同的shell窗口。但没有其他IP地址可用:mongo 10.0.0.96:3001/meteor

2015-11-16T10:16:14.089-0500 W NETWORK  Failed to connect to 10.0.0.96:3001, reason: errno:61 Connection refused
2015-11-16T10:16:14.091-0500 E QUERY    Error: couldn't connect to server 10.0.0.96:3001 (10.0.0.96), connection attempt failed
    at connect (src/mongo/shell/mongo.js:181:14)
    at (connect):1:6 at src/mongo/shell/mongo.js:181
exception: connect failed

====================== 附加设置信息====================

创建一个基本的流星应用:meteor create app cd app meteor 。在同一文件夹app中的另一个终端中运行meteor mongo -U以获取连接字符串:mongodb://127.0.0.1:3001/meteor。现在在另一个文件夹中创建一个想要连接到mongo db的节点应用程序:mkdir workercd workernpm install mongodb,创建worker.js:

var MongoClient = require('mongodb').MongoClient;

// Use connect method to connect to the Server
MongoClient.connect("mongodb://127.0.0.1:3001/meteor", function(err, db) {

    if (err){
        console.log(err);
    }else{
        console.log("Connected correctly to server");
    }

});

最后在docker中运行:

docker run --rm -v "$(pwd)":/worker -w /worker iron/images:node-4.1 sh -c "node worker.js"

docker IP地址是192.168.99.100 我在ifconfig中列出的其中一个IP地址是

vboxnet0: flags=8943<UP,BROADCAST,RUNNING,PROMISC,SIMPLEX,MULTICAST> mtu 1500
    ether 0a:00:27:00:00:00 
    inet 192.168.99.1 netmask 0xffffff00 broadcast 192.168.99.255

我将其插入,运行docker命令并获取此信息:

{ [MongoError: connect ECONNREFUSED 192.168.99.1:3001]
  name: 'MongoError',
  message: 'connect ECONNREFUSED 192.168.99.1:3001' }

1 个答案:

答案 0 :(得分:3)

因此,假设您在本地Mac上运行meteor应用程序,然后您的节点应用程序的docker容器正在docker-machine中运行。可能是VirtualBox?如果您在MacOS(boot2docker)上使用默认的docker-machine

问题是,在MacOS计算机上的VM中运行的容器不一定与MacOS计算机实际所在的网络通信(使用127.0.0.1localhost

例如来自我的Mac:

$ ifconfig
...
vboxnet0: flags=8842<BROADCAST,RUNNING,SIMPLEX,MULTICAST> mtu 1500
ether 0a:00:27:00:00:00
vboxnet1: flags=8842<BROADCAST,RUNNING,SIMPLEX,MULTICAST> mtu 1500
ether 0a:00:27:00:00:01
vboxnet2: flags=8943<UP,BROADCAST,RUNNING,PROMISC,SIMPLEX,MULTICAST> mtu 1500
ether 0a:00:27:00:00:02
inet 192.168.99.1 netmask 0xffffff00 broadcast 192.168.99.255
vboxnet3: flags=8842<BROADCAST,RUNNING,SIMPLEX,MULTICAST> mtu 1500
ether 0a:00:27:00:00:03
...

您看到VirtualBox网络vboxnet2192.168.99.x范围。如果你进入docker-machine

$ docker-machine ssh default
                        ##         .
                  ## ## ##        ==
               ## ## ## ## ##    ===
           /"""""""""""""""""\___/ ===
      ~~~ {~~ ~~~~ ~~~ ~~~~ ~~~ ~ /  ===- ~~~
           \______ o           __/
             \    \         __/
              \____\_______/
 _                 _   ____     _            _
| |__   ___   ___ | |_|___ \ __| | ___   ___| | _____ _ __
| '_ \ / _ \ / _ \| __| __) / _` |/ _ \ / __| |/ / _ \ '__|
| |_) | (_) | (_) | |_ / __/ (_| | (_) | (__|   <  __/ |
|_.__/ \___/ \___/ \__|_____\__,_|\___/ \___|_|\_\___|_|
Boot2Docker version 1.8.1, build master : 7f12e95 - Thu Aug 13 03:24:56 UTC 2015
Docker version 1.8.1, build d12ea79
docker@default:~$ ifconfig
docker0   Link encap:Ethernet  HWaddr 02:42:2C:33:C7:80
          inet addr:172.17.42.1  Bcast:0.0.0.0  Mask:255.255.0.0
          inet6 addr: fe80::42:2cff:fe33:c780/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:1031 errors:0 dropped:0 overruns:0 frame:0
          TX packets:1755 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:62290 (60.8 KiB)  TX bytes:2561234 (2.4 MiB)

eth0      Link encap:Ethernet  HWaddr 08:00:27:CC:FD:18
          inet addr:10.0.2.15  Bcast:10.0.2.255  Mask:255.255.255.0
          inet6 addr: fe80::a00:27ff:fecc:fd18/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:482311 errors:0 dropped:0 overruns:0 frame:0
          TX packets:209033 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:401054787 (382.4 MiB)  TX bytes:12923400 (12.3 MiB)

eth1      Link encap:Ethernet  HWaddr 08:00:27:E6:C7:20
          inet addr:192.168.99.100  Bcast:192.168.99.255  Mask:255.255.255.0
          inet6 addr: fe80::a00:27ff:fee6:c720/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:47929 errors:0 dropped:0 overruns:0 frame:0
          TX packets:47777 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:3259250 (3.1 MiB)  TX bytes:10663711 (10.1 MiB)

您看到您的容器网是172.17.x.x。因此,任何容器都属于该网络,并且不一定能够使用127.0.0.1localhost

路由到另一个容器

好的,有一种方法可以访问主机上的mongodb(MacOS)Docker容器可以通过vboxnet2 ip地址192.168.99.1看到它,但这里的问题是mongodb必须绑定到0.0.0.0(任何IP)而不是127.0.0.1。因此,如果您查看Mac上使用meteor运行的默认mongodb。

 $ ps -Af | grep mongo
  503 75294 73105   0  6:08PM ttys005    0:00.00 grep mongo
  503 74375 74356   0  5:55PM ttys006    0:02.17 /Users/username/.meteor/packages/meteor-tool/.1.1.9.1sd3e7j++os.osx.x86_64+web.browser+web.cordova/mt-os.osx.x86_64/dev_bundle/mongodb/bin/mongod --bind_ip 127.0.0.1 --smallfiles --port 3001 --dbpath /Users/username/work/meteor/app/.meteor/local/db --oplogSize 8 --replSet meteor

你看到它绑定到IP 127.0.0.1所以当你从容器中尝试时:

user@b4d40a25f67d:/worker#:/worker# telnet 192.168.99.1 3001

你没有回应。

这里的黑客只是绑定到0.0.0.0(不推荐用于制作):

~/.meteor/packages/meteor-tool/1.1.9/mt-os.osx.x86_64/tools/runners/run-mongo.js


-   '--bind_ip', '127.0.0.1', '--smallfiles', '--port', port, '--dbpath', dbPath,
+   '--bind_ip', '0.0.0.0', '--smallfiles', '--port', port, '--dbpath', dbPath,

然后voilá:

user@b4d40a25f67d:/worker# telnet 192.168.99.1 3001
Trying 192.168.99.1...
Connected to 192.168.99.1.
Escape character is '^]'.

然后,您需要在worker.js中修改此行

MongoClient.connect("mongodb://192.168.99.1:3001/meteor", function(err, db) {

我很高兴地说它有效! :

$ docker run --rm -v "$(pwd)":/worker -w /worker iron/images:node-4.1 sh -c 'node worker.js'
Connected correctly to server