流星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
======================
附加设置信息====================
创建一个基本的流星应用程序:流星创建应用程序cd应用程序流星
.在同一文件夹应用程序的另一个终端中,运行meteor mongo -U获取连接字符串:mongodb://127.0.0.1:3001 / meteor.现在,在另一个文件夹中创建一个想要连接到mongo db的节点应用程序:mkdir worker,cd worker,npm install mongodb,create 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"
泊坞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' }
问题是,在MacOS计算机上的VM中运行的容器不一定能与MacOS计算机实际所在的网络(使用127.0.0.1或localhost)进行通信.
例如从我的Mac:
$ifconfig
...
vBoxnet0: flags=8842<BROADCAST,MULTICAST> mtu 1500
ether 0a:00:27:00:00:00
vBoxnet1: flags=8842<BROADCAST,MULTICAST> mtu 1500
ether 0a:00:27:00:00:01
vBoxnet2: flags=8943<UP,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,MULTICAST> mtu 1500
ether 0a:00:27:00:00:03
...
您会看到VirtualBox net vBoxnet2在192.168.99.x范围内.如果您使用SSH进入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.1或localhost路由到另一个容器
好的,所以有一种方法可以访问主机(MacOS)上的mongodb.docker容器可以通过vBoxnet2 ip地址192.168.99.1看到它,但这里的问题是mongodb必须绑定到0.0.0.0(任何IP)为127.0.0.1.因此,如果您查看Mac上运行流星的默认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',
然后瞧瞧:
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",db) {
我很高兴地说它有效! :
$docker run --rm -v "$(pwd)":/worker -w /worker iron/images:node-4.1 sh -c 'node worker.js'
Connected correctly to server