【问题标题】:Can't connect to Tarantool container inside Docker through Nodejs无法通过 Nodejs 连接到 Docker 内的 Tarantool 容器
【发布时间】:2017-12-08 18:45:39
【问题描述】:

我想使用以下代码连接到 tarantool 容器:

import TarantoolConnection from 'tarantool-driver'
let connection = new TarantoolConnection('192.168.99.100:3301');
connection.ping().then((res) => {
   console.log(res);
});

在那之前我启动了容器:

docker run -p 3301:3301 -d tarantool/tarantool:1.6

但结果我一无所获。

如果我尝试为这个空间创建空间或\和索引:

connection.eval("box.schema.space.create('myspace', {if_not_exists=true, temporary=true})").then((res) => {
    console.log(res);
});

我收到此错误:

UnhandledPromiseRejectionWarning:未处理的承诺 拒绝(拒绝 id:1):错误:此套接字已关闭

或:

UnhandledPromiseRejectionWarning:未处理的承诺 拒绝(拒绝 id:2):错误:连接将被破坏或 已经销毁,再创建一个

我从错误中看到,所需的套接字已经关闭,但我不明白为什么。

tarantool 驱动版本:

"tarantool-driver": "2.0.5",

我该如何解决?

【问题讨论】:

    标签: javascript node.js docker tarantool


    【解决方案1】:

    这里有两个问题:

    1. 您应该连接到localhost:3301 而不是192.168.99.100:3301
    2. 您必须在connection.ping()connection.eval() 之前使用connection.connect()

    这是工作代码:

    const TarantoolConnection = require('tarantool-driver');
    
    let connection = new TarantoolConnection({port: 3301});
    
    connection.connect().then((res) => {
        console.log("Connected: " + res);
    
        connection.ping().then((res) => {
            console.log("Pong: " + res);
        });
    
        connection.eval("box.schema.space.create('myspace', {if_not_exists=true, temporary=true})").then((res) => {
            console.log("Space created");
        });
    });
    

    以防万一,我使用以下命令启动 tarantool docker 实例:

    $ docker run --rm -p 3301:3301 -t -i tarantool/tarantool:1.6
    

    【讨论】:

    • 不幸的是,我仍然有同样的错误:UnhandledPromiseRejectionWarning:未处理的承诺拒绝(拒绝 id:1):错误:连接 ECONNREFUSED 127.0.0.1:3301。其他容器工作正常,我可以连接到它们
    • 这很奇怪。看起来 tarantool 驱动程序无法连接到 tarantool docker 容器。在运行 tarantool 容器的情况下,您能否尝试在另一个 shell 窗口中使用 telnet localhost 3301 连接它并告诉我结果?
    • 这真的很奇怪,因为我什至无法使用 telnet 连接。我收到错误:无法打开与主机的连接,使用端口 3301:连接错误
    • 但是当我尝试连接 uisng docker ip 192.168.99.100 它连接成功(它可以这样调用,因为我收到类似Tarantool 1.6.9 (Binary) and strange sequence of symbols的消息)
    • 嗯,上面的信息是OK的,说明可以连接tarantool了。将我上面代码的第三行{port: 3301} 更改为{host: '192.168.99.100', port: 3301} 并运行它。它应该工作。另一个问题为什么端口映射到本地主机不起作用。也许您的 docker 设置有问题。您能解释一下您的操作系统和 docker 配置吗?
    猜你喜欢
    • 2020-03-28
    • 1970-01-01
    • 2017-08-10
    • 1970-01-01
    • 2019-06-20
    • 1970-01-01
    • 1970-01-01
    • 2022-01-11
    • 2017-05-04
    相关资源
    最近更新 更多