无法通过 Nodejs 连接到 Docker 内的 Tarantool 容器

Can't connect to Tarantool container inside Docker through Nodejs

我想使用此代码连接到 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

但结果我一无所获

如果我尝试为此 space 创建 space or\and 索引:

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

我收到这个错误:

UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: This socket is closed

或:

UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): Error: connection will be destroyed or already destroyed, create another one

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

tarantool 驱动程序版本:

"tarantool-driver": "2.0.5",

我该如何解决?

你这里有两个问题:

  1. 您应该连接到 localhost:3301 而不是 192.168.99.100:3301
  2. 您必须在 connection.ping()connection.eval()
  3. 之前使用 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